0
votes

In the code above uitablewidget does not update using signal and slot. It seems as if (ui->tableWidget->setItem(0,0,newItemx);) doesn't work. Am I doing something wrong or is there a better way to update my qtablewidget from my class B?

Class_A::Class_A(QWidget *parent):QDialog(parent),ui(new Ui::Class_A)
{
ui->setupUi(this);

}

Class_A::~Class_A()
{
delete ui;
}


void Class_A::change_TableWidget(double x,double y) // this is the    public slot
{
QTableWidgetItem *newItemx = new QTableWidgetItem(QString::number(x));
ui->tableWidget->setItem(0,0,newItemx);
QTableWidgetItem *newItemy = new QTableWidgetItem(QString::number(y));
ui->tableWidget->setItem(0,0,newItemy);
}




Class_B::Class_B(QWidget *parent) :
 QGLWidget(parent)
{
Class_A *t=new Class_A;
connect(this,SIGNAL(mySignal(double,double)),t,SLOT(change_TableWidget(double,double)));
}


void Class_B::mousePressEvent(QMouseEvent *event)
{
double x = event->x();
double y = event->y();
emit mySignal(x,y);
}
1
Is change_TableWidget called at all? You should check this first. - Greenflow
The command ui->tableWidget->setItem(0,0,newItemx); works fine in the constructor of the class, and i have also ckecked that my function (change_TableWidget(double x,double y)) is called. - George

1 Answers

1
votes

You don't have a SLOT(change_TableWidget(double,double)) - yours takes 3 doubles, not two.

You should check that connect() returned true. I like to write if (!connect(....)) Q_ASSERT(false);

or if (!connect(....)) Q_ASSERT(!"connect");

Also, connect prints out messages to the debug output when it fails to match the signals and slots. You should look for that output.

(Or use the new Qt 5 connect(), which is all checked at compile time.)