PDA

View Full Version : Accessing check state of CheckBox in QTableWidget



lnxusr
21st November 2009, 00:32
I have a tablewidget where one item contains a checkbox. The state of the box is known when I create it like so:


QCheckBox *tblcbx= new QCheckBox();
m_ui->tableWidget->setCellWidget(row, 6, tblcbx);
if(m_ui->checkBox->isChecked())
tblcbx->setChecked(true);

My table is set to allow selection of entire row. I access the row via the itemSelectionChanged slot when a row is clicked on like so:


void Generate::on_tableWidget_itemSelectionChanged()
{
QList<QTableWidgetItem*> selected(m_ui->tableWidget->selectedItems());
QTableWidgetItem *item;

// Load the number boxes and string(list) with respective numbers..
foreach(item, selected)
{
Numbers[item->column()]->setText(item->text());

if(item->column() < 5)
WBNumbers << item->text();
}
PBNumber = selected[5]->text();
}

This works fine, and all items get to where they're supposed to go when a row is selected, but I can't for the life of me figure out how to read the checkstate of the checkbox in column 6.

On a similar note, how do I unselect rows in the tablewidget? Say, if the selected row was deleted, the widget will highlight the next or previous row. I don't want that. I want no rows highlighted after deletion, or when that row is clicked on again, it will unhighlight.

Also, is it possible to set it so only one row can be selected at a time (ie: holding down the CTRL key will not allow selecting two or more rows)?

Tanuki-no Torigava
21st November 2009, 00:49
Looks like you forget about numbering. It starts from 0. Cheers.

lnxusr
21st November 2009, 01:24
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.

faldzip
21st November 2009, 10:58
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:


QCheckBox *cb = qobject_cast<QCheckBox *>(m_ui->tableWidget->cellWidget(selected[5]->row(), selected[5]->column()));


So I hope you can now determine the checkstate of that checkbox.

lnxusr
21st November 2009, 22:22
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,


QCheckBox *cb = qobject_cast<QCheckBox *>(m_ui->tableWidget->cellWidget(selected[6]->row(), selected[6]->column()));

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:


QCheckBox *mcb = qobject_cast<QCheckBox*>(m_ui->tableWidget->cellWidget(item->row(), 6));

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.

faldzip
22nd November 2009, 01:14
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?

lnxusr
22nd November 2009, 02:13
Not a segfault, I get this:

ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/qt4/QtCore/qlist.h, line 403

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,

QCheckBox *mcb = qobject_cast<QCheckBox*>(m_ui->tableWidget->cellWidget(item->row(), 6));

but I can't get column six using


QCheckBox *mcb = qobject_cast<QCheckBox*>(m_ui->tableWidget->cellWidget(item->row(), selected[6]->column()));

Why!?

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