Results 1 to 5 of 5

Thread: How to get signal when checkbox in table cell changed?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default 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....

    Qt Code:
    1. QCheckBox *cb1 = new QCheckBox(this);
    2. QWidget * w = new QWidget();
    3. l->setAlignment(Qt::AlignCenter);
    4. l->addWidget(cb1);
    5. w->setLayout(l);
    6. table->setCellWidget(ctr,6, w);
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. connect(table, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(tableitemchanged(QTableWidgetItem *)));
    2. connect(table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableitemchanged(QTableWidgetItem *)));
    To copy to clipboard, switch view to plain text mode 
    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????

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. cb1->setProperty("row", ctr);
    2. cb1->setProperty("column", 6);
    3. connect(cb1, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled(bool)));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MyClass::checkBoxBoxToggled(bool on)
    2. {
    3. QObject *cb = sender();
    4. const int row = cb->property("row");
    5. const int column = cb->property("column");
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    d_stranz (11th August 2016)

  4. #3
    Join Date
    Aug 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get signal when checkbox in table cell changed?

    THANK YOU!
    That worked perfectly.





    Quote Originally Posted by anda_skoa View Post
    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

    Qt Code:
    1. cb1->setProperty("row", ctr);
    2. cb1->setProperty("column", 6);
    3. connect(cb1, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled(bool)));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MyClass::checkBoxBoxToggled(bool on)
    2. {
    3. QObject *cb = sender();
    4. const int row = cb->property("row");
    5. const int column = cb->property("column");
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #4
    Join Date
    Jul 2019
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get signal when checkbox in table cell changed?

    The original code is missing the conversion to int

    Qt Code:
    1. const int row = cb->property("row").toInt();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Detect when the content of a cell of QTableView is changed
    By qt_developer in forum Qt Programming
    Replies: 5
    Last Post: 21st August 2021, 17:00
  2. How to detect when item is changed in the table view?
    By schmimona in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2011, 10:06
  3. Replies: 1
    Last Post: 8th June 2011, 15:13
  4. Replies: 1
    Last Post: 19th May 2010, 16:10
  5. QTreeView: How to center a checkbox in a cell
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 13:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.