PDA

View Full Version : How to get signal when checkbox in table cell changed?



ealbers
11th August 2016, 14:27
I have created a checkbox in a QTableWidget cell and it looks good, it checks/unchecks when I click it....



QCheckBox *cb1 = new QCheckBox(this);
QWidget * w = new QWidget();
QHBoxLayout *l = new QHBoxLayout();
l->setAlignment(Qt::AlignCenter);
l->addWidget(cb1);
w->setLayout(l);
table->setCellWidget(ctr,6, w);


Now I'd like to get a slot called when the user clicks on the checkbox in the table cell...
I need of course, to know the row which was clicked and the column,

I have tried, cellClicked, cellDoubleClicked, <--work only when clicking on other things, don't work with checkbox.
I have connected to table->model() dataChanged, this also is NOT called when the checkbox is clicked,
I tried adding a


connect(table, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(tableitemchanged(QTableWidgetItem *)));
connect(table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableitemchanged(QTableWidgetItem *)));

Both of which never trigger no matter what I click on anywhere in the table (even changing text values).

SO....
We can add checkboxes to tables....but how do we get a message when said checkbox is clicked on????

anda_skoa
11th August 2016, 15:26
Since you know row and column when creating the checkbox, I would just store these values as dynamic properties on the object and connect to the checkbox' toggled signal



cb1->setProperty("row", ctr);
cb1->setProperty("column", 6);
connect(cb1, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled(bool)));



void MyClass::checkBoxBoxToggled(bool on)
{
QObject *cb = sender();
const int row = cb->property("row");
const int column = cb->property("column");
}


Cheers,
_

ealbers
11th August 2016, 17:55
THANK YOU!
That worked perfectly.






Since you know row and column when creating the checkbox, I would just store these values as dynamic properties on the object and connect to the checkbox' toggled signal



cb1->setProperty("row", ctr);
cb1->setProperty("column", 6);
connect(cb1, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled(bool)));



void MyClass::checkBoxBoxToggled(bool on)
{
QObject *cb = sender();
const int row = cb->property("row");
const int column = cb->property("column");
}


Cheers,
_

PradeepP
16th July 2019, 07:37
I am using Qt 5.12.3 on windows. When i implemented 'checkboxtoggled()' function it gives compilation error - 'error: cannot convert 'QVariant' to 'const int' in initialization' for statement 'const int column = cb->property("column");'.
Please help to resolve this error.

anda_skoa
16th July 2019, 08:31
The original code is missing the conversion to int



const int row = cb->property("row").toInt();


Cheers,
_