View Full Version : Best practices concerning QStandardItemModel

14th January 2010, 21:53

I have a set of custom widgets that each contain a QTableView and an underlying QStandardItemModel. Some of these widgets are dependant on the models contained within other widgets. Currently, I am simply passing around QStandardItemModel pointers to achieve this. As such, the dependant widgets need to know what each model it's using "looks" like (headers, # of rows/columns, etc.).

Note: that the dependant widgets also need to respond to changes in the data they are depending on. Currently this is achieved by connecting a signal from a widget to a private slot of a dependant widget.

A possible change would be to subclass QStandardItemModel and provide a data specific interface to the model. For example, if I had a table that stored car information, clients would use:

Car *car = model->getCar(row)
int numSeats = car->numSeats;

instead of:

QStandardItem *item = model->item(row, col);
int numSeats = item->data(Qt::DisplayRole).toInt();

I have several tables, so creating a wrapper interface for each one seems a little overkill, but at the same time the way I am currently doing things seems a little error prone.

What is the best practice in this case?
Any suggestions and ideas appreciated!


15th January 2010, 00:17
It sounds like you're trying to recreate the model-view wheel that Qt already provides: Model View Programming (http://doc.qt.nokia.com/4.6/model-view-programming.html).

The benefit to using models is that you could have several views share the data contained in one model - if data changes in the underlying model, then all of the views attached to that model get updated as well.

Hope this helps.

I forgot to add that if several views need tailored information from the model, then you can develop proxy models for each view to hide/customize the information. That may help alleviate the messiness you're experiencing.

27th January 2010, 06:29
Thanks for the suggestions.
Using proxy models has helped resolve some of the issues I was having before.