How to reference a indexWidget in a QTableView
-
I'm building a small QTableView (only 20 rows) with two columns: Name and Include. I want to use a QCheckBox in the include column, which is a bool type.
ui->infoFieldsTable->setModel(mw->infoView->ok); for(int row = 0; row < mw->infoView->ok->rowCount(); row++) { QModelIndex idx = mw->infoView->ok->index(row,1); ui->infoFieldsTable->setIndexWidget(idx, new QCheckBox); // set state to match data model value ui->infoFieldsTable->indexWidget(idx); // this is a QWidget, not a QCheckbox, so setCheckState not available }
This works to the extent that the table shows the data model information and I can update the data model if I do not add the QCheckBox.
How do I reference the QCheckBox to update it to the data model value?
-
Hi
The items supports checkbox with Qt::ItemIsUserCheckable
for the flags function.Qt::ItemFlags yourmodel::flags(const QModelIndex &index) const { Qt::ItemFlags result = yourbasemodel::flags(index); if (index.column() == COL_U_WANT ) { result |= Qt::ItemIsUserCheckable; } return result; }
That said
it just return as QWidget but you can cast it
QWidget *wid= ui->infoFieldsTable->indexWidget(idx);
QCheckBox *box= qobject_cast<QCheckBox *> ( wid )
if (box) {
box->xxxx
} -
Hi
The items supports checkbox with Qt::ItemIsUserCheckable
for the flags function.Qt::ItemFlags yourmodel::flags(const QModelIndex &index) const { Qt::ItemFlags result = yourbasemodel::flags(index); if (index.column() == COL_U_WANT ) { result |= Qt::ItemIsUserCheckable; } return result; }
That said
it just return as QWidget but you can cast it
QWidget *wid= ui->infoFieldsTable->indexWidget(idx);
QCheckBox *box= qobject_cast<QCheckBox *> ( wid )
if (box) {
box->xxxx
}