Unsolved tableWidget question
-
I want to set the first column as a text box, the second as a combo box and third as a check box.
Can I do it with the widget? -
@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
Didn't understand much from the documentation how I apply all this specifically to a tableWidget. -
@jenya7
AQTableWidget
is derived from aQAbstractItemView
so these methods are available (https://doc.qt.io/qt-5/qtablewidget-members.html). There is nothing special about it being aQTableWidget
. -
@JonB said in tableWidget question:
@jenya7
AQTableWidget
is derived from aQAbstractItemView
so these methods are available (https://doc.qt.io/qt-5/qtablewidget-members.html). There is nothing special about it being aQTableWidget
.It's all foreign language to me. I am completely new in Qt.
-
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.