tableWidget question
-
@jenya7
Hi
There is https://doc.qt.io/qt-5/qtablewidget.html#item
where you can access the items to check or uncheck it.But often you use some of the signals
like
https://doc.qt.io/qt-5/qtablewidget.html#itemChanged
and there you directly get the item.@mrjj said in tableWidget question:
@jenya7
Hi
There is https://doc.qt.io/qt-5/qtablewidget.html#item
where you can access the items to check or uncheck it.But often you use some of the signals
like
https://doc.qt.io/qt-5/qtablewidget.html#itemChanged
and there you directly get the item.Thank you.
-
I did so
void sys::ParamLoadToTable(QTableWidget *table, int rows) { QTableWidgetItem item; table->setRowCount(rows); for (int i = 0; i < rows; i++) { auto box = new QCheckBox(); box->setCheckState(Qt::Unchecked); table->setCellWidget(i, 3, box); item.setText(param_list[i].name); table->setItem(i, 1, &item); } }
I see all rows but column 1 (name) is empty.
-
OK. This way it works
for (int i = 0; i < rows; i++) { auto box = new QCheckBox(); box->setCheckState(Qt::Unchecked); table->setCellWidget(i, 3, box); QTableWidgetItem * item1 = new QTableWidgetItem(); table->setItem(i, 1, item1); QTableWidgetItem * item2 = new QTableWidgetItem(); table->setItem(i, 2, item2); item1->setText(param_list[i].name); item2->setText(param_list[i].value); }
-
@jenya7
Hi
You can use
https://doc.qt.io/qt-5/qtablewidget.html#cellWidget
to gain access to the checkbox. -
@jenya7
Hi
You can use
https://doc.qt.io/qt-5/qtablewidget.html#cellWidget
to gain access to the checkbox.@mrjj said in tableWidget question:
@jenya7
Hi
You can use
https://doc.qt.io/qt-5/qtablewidget.html#cellWidget
to gain access to the checkbox.QWidget *box = table->cellWidget(i, 3);
Then what? How do I get the check box state from the object?
-
@jenya7 said in tableWidget question:
How do I get the check box state from the object?
Try to cast it to a QCheckBox (with e.g. dynamic_cast or Qt's specific qobject_cast) and retrieve the information.
-
@mrjj said in tableWidget question:
@jenya7
Hi
You can use
https://doc.qt.io/qt-5/qtablewidget.html#cellWidget
to gain access to the checkbox.QWidget *box = table->cellWidget(i, 3);
Then what? How do I get the check box state from the object?
@jenya7
Hi
It returns a base Widget pointer so you need to cast it.QCheckBox * checkbox = qobject_cast<QCheckBox * > (table->cellWidget(i, 3)) ;
if (checkbox) { // must check. can be NULL
if ( checkbox->isChecked() ) ....
} -
@jenya7
Hi
It returns a base Widget pointer so you need to cast it.QCheckBox * checkbox = qobject_cast<QCheckBox * > (table->cellWidget(i, 3)) ;
if (checkbox) { // must check. can be NULL
if ( checkbox->isChecked() ) ....
}@mrjj said in tableWidget question:
@jenya7
Hi
It returns a base Widget pointer so you need to cast it.QCheckBox * checkbox = qobject_cast<QCheckBox * > (table->cellWidget(i, 3)) ;
if (checkbox) { // must check. can be NULL
if ( checkbox->isChecked() ) ....
}Thank you.
-
@jenya7
Yes, you should do so via https://doc.qt.io/qt-5/qabstractitemview.html#setItemDelegateForColumn with a https://doc.qt.io/qt-5/qstyleditemdelegate.html which you write for each of the desired column types.Although you might be tempted to use https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget, you will have @VRonin descending on you like a ton of bricks here if you do so :)
@JonB said in tableWidget question:
you might be tempted to use https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget, you will have @VRonin descending on you like a ton of bricks here if you do so
And here I am!
Also
QCheckBox
is the worst excuse to use this method. It's much easier to do without a widget.To activate a checkbox in a cell you can just call:
QTableWidgetItem *checkBoxitem = new QTableWidgetItem; checkBoxitem->setCheckState(Qt::Unchecked); // displays the checkbox as unchecked checkBoxitem->setFlags(checkBoxitem->flags() | Qt::ItemIsUserCheckable); // allows the user to click on the checkbox table->setItem(i, 3, checkBoxitem);
You can then check the item state using
item->checkState()
orwidget->model()->index(row,column).data(Qt::CheckStateRole)