Nominate our 2022 Qt Champions!

One QSqlTableModel, two different views

  • I know often the same set of very useful folks will reply to these posts, so you might be aware that I am working on an order form. The order form I am working on has a model, orderItemModel that derives from QSqlTableModel. One of the columns is 'notes'. In the main invoice dialog, the TableView simple shows either an icon ( aka decoration), one if there is a note, another if the note is empty.

    The icon is displayed by modifying the model's data function. It returns a different decoration depending on if the notes is null or not. Also, this model does NOT return the actual notes when the row, Qt::DisplayRole) is called.

    When the use clicks on the notes icon, a notes dialog will appear. I would like this notes dialog to act like the "SQL Widget Mapper Example":, where there is a next/previous button to change to different notes from the dialog.

    I would like to use the same model in both dialogs so they stay in sync for me, but the order dialog model is not returning the actual text, just a decoration.

    So how exactly can I have one base model and two different implementation of the data method?

    P.S. It all has to stay in memory because the orderItems always start off not being in the DB.

  • I would introduce an user role like 'ActualNoteContentRole' in your model subclass and use this for peeking and poking the actual text in your model.

    Then subclass a proxy model and set it as the model for your second view.

    In that proxy model, you make the "translation" of roles in reimplemented data() and setData() methods: if the view calls for DisplayRole or EditRole just read or set the contents of ActualNoteContentRole.

  • Your first Model is named FirstModel. Then you write a second Model which acts as a proxy to the first model and gives you in essence a second data method.

    @class ModifiedModel : public QAbstractTableModel
    ModifiedModel(FirstModel &firstModel); //Mind the argument
    int rowCount ( QModelIndex & parent = QModelIndex() ){firstModel.rowCount(parent); }
    data(.....// do your crazy stuff here

    This way you can have a customized presentation for each view.

  • Volker,

    That seems to make perfect sense, dumb down the current "QSqlTableModel": and create a "QProxyModel": for the order dialog, then pass the "QSqlTableModel": to the notes dialog.

    Thank you

  • Qproxymodel is obsolete you should use qabstractproxymodel or one of its descendants. You probably know that but because you use the link to the docs, maybe you don't.

  • Eddy,

    I did not know that, thank you.

  • Happy i could help. You can see it on line 7. But as we have to read a lot of docs, it's easy to mis it.

  • Am I missing something here? In the "QAbstractProxyModel": documentation it states:

    All standard proxy models are derived from the QAbstractProxyModel class. If you need to create a new proxy model class, it is usually better to subclass an existing class that provides the closest behavior to the one you want to provide.

    But it looks like there is only one class that derives from it: "QSortFilterProxyModel":

    Are there more or was this statement in anticipation of future derived works?


  • In Qt there is only qsortfilterproxymodel ... at this moment... ;)

    But other developers can make subclasses too. For instance when you program for kde. So in that sense the sentence is correct.

Log in to reply