I have a table in Sqlite database which I display using QTableview and QSqlQueryModel. The first column needs to have a header which is a checkbox and all the items in the c
The easiest way for displaying checkable items is using QStandardItemModel as QStandardItem can be set checkable. It is good for prototyping. The drawback however is, you had to fill the model manually.
I have found this solution that does not uses delegates or anything like that. You will still have a problem centering the checkbox. That is up to you.
The next code snippet will make a column filled with checkboxes:
yourSqlQueryModel = new QSqlQueryModel();
yourTableView = new QtableView();
yourSqlQueryModel ->setQuery(yourQuery);
yourSqlQueryModel ->insertColumn(0);//Insert column for checkboxes
ui->yourTableView ->setModel(yourSqlQueryModel );
ui->yourTableView ->resizeColumnsToContents();
int p;
for(p = 0;p<yourSqlQueryModel ->rowCount();p++)
{
ui->yourTableView ->setIndexWidget(yourSqlQueryModel ->index(p,0),new QCheckBox());
}
Please read carefully, the most important here is the setIndexWidget method, which allows you to insert the widget into the created column.