groenhen groenhen - 2 months ago 6
SQL Question

How to add a column in a table where I can put checkmarks in some cells for further processing?

I am using Qt5.
Now I have a

QTableView
populated with data from a database table using
QSqlTableModel
. I added an extra column with
model->insertColumn(n);
.
I would like to be able - using the mouse - to put some checkmarks in some cells of this new added column, as - after exiting the
tableView
- I would need to do some further processing for the rows that are marked with the checkmark...
Is there a way of doing this? Please help.

BAD news is: I can add/change all cells in the table (using
model->setData(index, "...");
), EXCEPT the cells of the newly added column...

Why is this happening?

Answer

That your QSqlTableModel is modifying (or trying to) the corresponding table in the database, and that's likely failing somehow. Is your extra column really corresponding to data coming from the database, or is it synthetized?

Anyhow, two possible solutions are

  1. a proxy model in front of your original SQL model
  2. subclass your SQL model

The idea is to use the proxy/subclass to augment the data (by synthetizing the extra column's data), as well as handle the checked status. In order to mark cells "checkable" you need to return Qt::ItemIsCheckable from your model's flags() implementation, then the view will call data() asking for the Qt::CheckStateRole.