Unsolved tableWidget question
-
Hi,
That is nothing specific to Qt. It's C++ inheritance.
-
Hi
A delegate is a helper class that can draw the cell and/or also allows editing.
Here is an example of a special one. Like a rating bar with stars
https://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.htmlYou can see how to make one for a combobox here
https://wiki.qt.io/Combo_Boxes_in_Item_Views
This way can be used with any Widget you want. In the CreateEditor you just create another one. rest of code is much the same.For the checkbox, you can actually set on the Items you insert that they can be checked ( like a checkbox)
QTableWidgetItem *item2 = new QTableWidgetItem("Item2"); item2->setCheckState(Qt::Checked); // or unchecked tableWidget->setItem(0, 0, item2);
-
This post is deleted! -
@jenya7 said in tableWidget question:
@mrjj said in tableWidget question:
Hi
A delegate is a helper class that can draw the cell and/or also allows editing.
Here is an example of a special one. Like a rating bar with stars
https://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.htmlYou can see how to make one for a combobox here
https://wiki.qt.io/Combo_Boxes_in_Item_Views
This way can be used with any Widget you want. In the CreateEditor you just create another one. rest of code is much the same.For the checkbox, you can actually set on the Items you insert that they can be checked ( like a checkbox)
QTableWidgetItem *item2 = new QTableWidgetItem("Item2"); item2->setCheckState(Qt::Checked); // or unchecked tableWidget->setItem(0, 0, item2);
Thank you. The first argument is a row number. But tableWidget is not populated yet. I start to add rows and on every row I should call tableWidget->setItem(row_num, 0, item2); ?
-
@jenya7
You can only do that once the row,row_num
, exists in the table. You will need to have called https://doc.qt.io/qt-5/qtablewidget.html#insertRow to insert the row(s) beforehand. -
Hi
This might come in handy
https://wiki.qt.io/How_to_Use_QTableWidget -
@mrjj said in tableWidget question:
Hi
This might come in handy
https://wiki.qt.io/How_to_Use_QTableWidgetThank you. One more question - after populating the table - how can I check/uncheck a check box in a row or to check a check box state?
-
@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); }
-
How can I check if a check box in the 4-th column is set?
-
@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.
-
@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.
-
@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)