Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. One QSqlTableModel, two different views
Forum Update on Tuesday, May 27th 2025

One QSqlTableModel, two different views

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scarleton
    wrote on 7 Jul 2011, 11:21 last edited by
    #1

    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 orderItemModem.data( 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":http://doc.qt.nokia.com/4.7/sql-sqlwidgetmapper.html, 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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 7 Jul 2011, 11:36 last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dialingo
        wrote on 7 Jul 2011, 13:15 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          scarleton
          wrote on 7 Jul 2011, 13:25 last edited by
          #4

          Volker,

          That seems to make perfect sense, dumb down the current "QSqlTableModel":http://doc.qt.nokia.com/4.7/qsqltablemodel.html and create a "QProxyModel":http://doc.qt.nokia.com/4.7/qproxymodel.html for the order dialog, then pass the "QSqlTableModel":http://doc.qt.nokia.com/4.7/qsqltablemodel.html to the notes dialog.

          Thank you

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Eddy
            wrote on 7 Jul 2011, 13:42 last edited by
            #5

            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.

            Qt Certified Specialist
            www.edalsolutions.be

            1 Reply Last reply
            0
            • S Offline
              S Offline
              scarleton
              wrote on 7 Jul 2011, 13:46 last edited by
              #6

              Eddy,

              I did not know that, thank you.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Eddy
                wrote on 7 Jul 2011, 13:51 last edited by
                #7

                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.

                Qt Certified Specialist
                www.edalsolutions.be

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  scarleton
                  wrote on 7 Jul 2011, 13:52 last edited by
                  #8

                  Am I missing something here? In the "QAbstractProxyModel":http://doc.qt.nokia.com/4.7/qabstractproxymodel.html 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":http://doc.qt.nokia.com/4.7/qabstractproxymodel.html

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

                  Sam

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    Eddy
                    wrote on 7 Jul 2011, 14:00 last edited by
                    #9

                    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.

                    Qt Certified Specialist
                    www.edalsolutions.be

                    1 Reply Last reply
                    0

                    2/9

                    7 Jul 2011, 11:36

                    topic:navigator.unread, 7
                    • Login

                    • Login or register to search.
                    2 out of 9
                    • First post
                      2/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved