PDA

View Full Version : What is the best way to support CheckBox in a QSqlTableModel ?



sOuSiX
20th April 2011, 17:57
Hello all,

I'm searching for a "generic" solution to store checkbox status in a QSqlTableModel. In fact, I have a custom QSqlTableModel with a QSet<QPersistenModelIndex> to store all modelindex where the box are checked.
Of course i have redefined data(), setData(), flag(), removeRows().
--> My simple implementation is here on github :
sqltablemodelcheckable.cpp (https://github.com/sOuSiX/QDM68/blob/master/src/sqltablemodelcheckable.cpp)
sqltablemodelcheckable.h (https://github.com/sOuSiX/QDM68/blob/master/src/sqltablemodelcheckable.h)

The problem is : when I call "SubmitAll()", all my QPersistenModelIndex are automatically set to invalid. Are the modelindex that they refers are deleted, then recreated or something like that (???)

Does someone know why ? Or can indicate me another solution.
Thanks a lot.

ChrisW67
21st April 2011, 01:31
When you submitAll() all selections and indexes are invalidated and the model is repopulated from the underlying database. The new query might return rows in a different order, more rows, or less rows (e.g. if another process has removed rows in the meantime) so a row-column pair (QModelIndex or QPersistentModelIndex) may not be pointing at the same data afterward.

You would need to store a unique row id from your data and column for each checked cell before the submit and recheck them after the submit. Alternatively, you could store the checked columns as a bitmap in an extra column of the table.

sOuSiX
21st April 2011, 09:25
Thanks =)