Best approach to handle cell properties with model
-
I have my own class that inherits
QAbstractTableModel
.
The model is filled using data read from a csv file.Then I need to cycle among all rows and columns and set some "properties" for each field (cell).
Example: for one column I need to query a database to search something and if I find it I will set the property "exists" to true, otherwise "false". Using theQt::BackgroundColorRole
I will change the background to green or red according.Questions:
- this "parsing" operation should be executed by the model class or by the higher level one (i.e. the
QDialog
that shows the table) ? - if it's ok to do the parsing within the model class, may I avoid to add hidden columns to store the properties values and add them to the data struct only? This is because if I have, say, 3 properties for each field I need to append three times the column count!
- this "parsing" operation should be executed by the model class or by the higher level one (i.e. the
-
@Mark81 said in Best approach to handle cell properties with model:
this "parsing" operation should be executed by the model class or by the higher level one (i.e. the QDialog that shows the table) ?
I'd say it belongs in the model. The view classes are and should be "dumb", they should only display what model tells them.
if it's ok to do the parsing within the model class, may I avoid to add hidden columns to store the properties values and add them to the data struct only? This is because if I have, say, 3 properties for each field I need to append three times the column count!
Yep, you can keep that metatada internally, no need for hidden columns. Exactly how you do it will depend on your requirements (if this operation is cheap, you can put the parsing in your model's
data()
method, if it takes time it will be better to do it once at startup, and store the results in some helper data structure). -
@sierdzio said in Best approach to handle cell properties with model:
Yep, you can keep that metatada internally, no need for hidden columns. Exactly how you do it will depend on your requirements (if this operation is cheap, you can put the parsing in your model's
data()
method, if it takes time it will be better to do it once at startup, and store the results in some helper data structure).Got it! Thanks!