How to get signal when checkbox in table cell changed?
I have created a checkbox in a QTableWidget cell and it looks good, it checks/unchecks when I click it....
Code:
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
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????
Re: How to get signal when checkbox in table cell changed?
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
Code:
cb1->setProperty("row", ctr);
cb1->setProperty("column", 6);
connect(cb1, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled(bool)));
Code:
void MyClass::checkBoxBoxToggled(bool on)
{
const int row = cb->property("row");
const int column = cb->property("column");
}
Cheers,
_
Re: How to get signal when checkbox in table cell changed?
THANK YOU!
That worked perfectly.
Quote:
Originally Posted by
anda_skoa
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
Code:
cb1->setProperty("row", ctr);
cb1->setProperty("column", 6);
connect(cb1, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled(bool)));
Code:
void MyClass::checkBoxBoxToggled(bool on)
{
const int row = cb->property("row");
const int column = cb->property("column");
}
Cheers,
_
Re: How to get signal when checkbox in table cell changed?
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.
Re: How to get signal when checkbox in table cell changed?
The original code is missing the conversion to int
Code:
const int row = cb->property("row").toInt();
Cheers,
_