Results 1 to 7 of 7

Thread: Accessing check state of CheckBox in QTableWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: Accessing check state of CheckBox in QTableWidget

    No, I didn't forget about numbering. If you'll notice, I'm reading cells 0 thru 4 into a stringlist via the foreach statement and cell 5 into a string on it's own right after. That leaves me with cell 6, which is the checkbox that I need to determine the state of. The first code snippet shows that the checkbox is indeed in cell 6.

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: Accessing check state of CheckBox in QTableWidget

    you have to get cell widget from QTableWidget with QTableWidget::cellWidget() - I wonder how you could miss that method in docs as you found setCellWidget() (cellWidget() is in 'see also' note in description of setCellWidget()). So you can very easily get your checkbox:
    Qt Code:
    1. QCheckBox *cb = qobject_cast<QCheckBox *>(m_ui->tableWidget->cellWidget(selected[5]->row(), selected[5]->column()));
    To copy to clipboard, switch view to plain text mode 

    So I hope you can now determine the checkstate of that checkbox.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: Accessing check state of CheckBox in QTableWidget

    Thanks faldżip,

    I did see the cellWidget method, but it wasn't working the way I was using it (which was similar to the line you gave me). That made me think it wasn't the way to access the checkbox. Your line made me look further and this is what I found.

    If I use 'selected' to get at the row or column,

    Qt Code:
    1. QCheckBox *cb = qobject_cast<QCheckBox *>(m_ui->tableWidget->cellWidget(selected[6]->row(), selected[6]->column()));
    To copy to clipboard, switch view to plain text mode 

    the program halts on a signal at that line.

    The only way I found that works is to hard code the column in there and use item-row() to get the row:

    Qt Code:
    1. QCheckBox *mcb = qobject_cast<QCheckBox*>(m_ui->tableWidget->cellWidget(item->row(), 6));
    To copy to clipboard, switch view to plain text mode 

    Six is the correct column. There are seven columns in my table.

    This is no biggie, as the column that hold the checkbox won't change, but I'd like to know if there's a problem in the way I'm implementing on_tableWidget_itemSelectionChanged that is causing this.

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 140 Times in 132 Posts

    Default Re: Accessing check state of CheckBox in QTableWidget

    the program halts on a signal at that line
    you mean you get segmentation fault? So check which of these many function calls in this line causes this seg fault. What item->column() returns?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: Accessing check state of CheckBox in QTableWidget

    Not a segfault, I get this:
    Qt Code:
    1. ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/qt4/QtCore/qlist.h, line 403
    To copy to clipboard, switch view to plain text mode 

    when I try to use sender[6] to get the row and/or column.

    This looks to me that ther is no sixth column in the table, but there is. I can force it to get column six by hardcodeing in a 6,
    Qt Code:
    1. QCheckBox *mcb = qobject_cast<QCheckBox*>(m_ui->tableWidget->cellWidget(item->row(), 6));
    To copy to clipboard, switch view to plain text mode 

    but I can't get column six using
    Qt Code:
    1. QCheckBox *mcb = qobject_cast<QCheckBox*>(m_ui->tableWidget->cellWidget(item->row(), selected[6]->column()));
    To copy to clipboard, switch view to plain text mode 

    Why!?

    Had to wrap the Assert in code or the second colon after QList<T> showed up as a smilie..

Similar Threads

  1. Replies: 3
    Last Post: 1st April 2011, 04:58
  2. how to check button state such as "hover"?
    By billconan in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2009, 01:45
  3. Help: How to save Check box state
    By Garibalde in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2009, 20:24
  4. Replies: 2
    Last Post: 5th June 2008, 21:32
  5. Replies: 0
    Last Post: 2nd May 2008, 07:57

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
  •  
Qt is a trademark of The Qt Company.