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. Custom Table Model
Forum Updated to NodeBB v4.3 + New Features

Custom Table Model

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 7 Posters 3.6k Views 3 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.
  • SGaistS SGaist

    Hi,

    @EaccB said in Custom Table Model:

    So are you saying that ideally I should only emit dataChanged() on the rows affected?

    Yes, this will avoid unneeded processing and updating of the GUI for things that did not change.

    E Offline
    E Offline
    EaccB
    wrote on last edited by EaccB
    #13

    @SGaist

    Ok, here's what I've done:

    void TestModel::update(int row){
    
       QModelIndex i = this->index(row,0,QModelIndex());
       QModelIndex p = this->index(row,1,QModelIndex());
    
       emit dataChanged(i,p);
    }
    

    This way, only the affected row is updated.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #14

      dataChange takes a range so here you always emit that the it starts from the first up to the row you pass as parameter. Is it really always the case ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 2 Replies Last reply
      0
      • SGaistS SGaist

        dataChange takes a range so here you always emit that the it starts from the first up to the row you pass as parameter. Is it really always the case ?

        E Offline
        E Offline
        EaccB
        wrote on last edited by
        #15

        @SGaist

        I edited my answer.

        1 Reply Last reply
        0
        • SGaistS SGaist

          dataChange takes a range so here you always emit that the it starts from the first up to the row you pass as parameter. Is it really always the case ?

          E Offline
          E Offline
          EaccB
          wrote on last edited by
          #16

          @SGaist

          When emitting dataChanged(i,p) for the deleted row only, how is it that the following rows move up? Wouldn't that require a dataChanged(i,p) for the whole table?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #17

            @EaccB said in Custom Table Model:

            When emitting dataChanged(i,p) for the deleted row only,

            You did not read my link. You must not call dataChanged() when removing a row.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            E 1 Reply Last reply
            4
            • Christian EhrlicherC Christian Ehrlicher

              @EaccB said in Custom Table Model:

              When emitting dataChanged(i,p) for the deleted row only,

              You did not read my link. You must not call dataChanged() when removing a row.

              E Offline
              E Offline
              EaccB
              wrote on last edited by
              #18

              @Christian-Ehrlicher

              Then what do I use to signal to the view that the model has been updated? I've removed dataChanged() and replaced it with layoutChanged() which seems to work fine.

              To avoid any confusion, the table itself is read-only and should only reflect the contents of the students vector inside StudentManager. Any changes to the data (i.e. add student, edit student, remove student) are done directly on the students vector.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #19

                @EaccB said in Custom Table Model:

                Then what do I use to signal to the view that the model has been updated?

                dataChanged() as explained above.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                E 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @EaccB said in Custom Table Model:

                  Then what do I use to signal to the view that the model has been updated?

                  dataChanged() as explained above.

                  E Offline
                  E Offline
                  EaccB
                  wrote on last edited by
                  #20

                  @Christian-Ehrlicher

                  Ok, I'm really confused. You said that I shouldn't use dataChanged() when removing a row. So, what should I do when an element is removed from the vector? Or are you saying that I should emit dataChanged from the class which owns the vector and connect it to a slot in the Model?

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #21

                    @EaccB said in Custom Table Model:

                    So, what should I do when an element is removed from the vector?

                    You really really should read the documentation. I already gave a the link two times...: https://doc.qt.io/qt-5/model-view-programming.html#inserting-and-removing-rows

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • E EaccB

                      @Christian-Ehrlicher

                      Ok, I'm really confused. You said that I shouldn't use dataChanged() when removing a row. So, what should I do when an element is removed from the vector? Or are you saying that I should emit dataChanged from the class which owns the vector and connect it to a slot in the Model?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #22

                      @EaccB
                      To reiterate what @Christian-Ehrlicher is telling you. The signal code in a model has to depend on whether you are updating an existing row which is already there and will remain there, versus if you inserting or deleting a row.

                      dataChanged() is only for same rows, with values in columns changing.

                      For inserting/deleting rows you must use begin/endInsert/RemoveRows() instead.

                      Views, for example, need to know this and react differently according as the model reports its rows are being updated or are being deleted/inserted. You can imagine the action they take will differ between these cases.

                      E 1 Reply Last reply
                      3
                      • JonBJ JonB

                        @EaccB
                        To reiterate what @Christian-Ehrlicher is telling you. The signal code in a model has to depend on whether you are updating an existing row which is already there and will remain there, versus if you inserting or deleting a row.

                        dataChanged() is only for same rows, with values in columns changing.

                        For inserting/deleting rows you must use begin/endInsert/RemoveRows() instead.

                        Views, for example, need to know this and react differently according as the model reports its rows are being updated or are being deleted/inserted. You can imagine the action they take will differ between these cases.

                        E Offline
                        E Offline
                        EaccB
                        wrote on last edited by
                        #23

                        @JonB

                        I think we got out wires crossed. By "update the vector", I meant add and remove elements, as well as editing existing items. This is why I was confused when @Christian-Ehrlicher suggested using dataChanged(), which he had previously told me not to use for removing elements.

                        If I add an element directly to the vector, do I still need to call insertRows() from the model?

                        VRoninV 1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by Christian Ehrlicher
                          #24

                          @EaccB said in Custom Table Model:

                          which he had previously told me not to use for removing elements.

                          where?

                          If I add an element directly to the vector, do I still need to call insertRows() from the model?

                          Yes, as clearly written in the docs.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          E 1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            @EaccB said in Custom Table Model:

                            which he had previously told me not to use for removing elements.

                            where?

                            If I add an element directly to the vector, do I still need to call insertRows() from the model?

                            Yes, as clearly written in the docs.

                            E Offline
                            E Offline
                            EaccB
                            wrote on last edited by
                            #25

                            @Christian-Ehrlicher

                            You must not call dataChanged() when removing a row.

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • E EaccB

                              @Christian-Ehrlicher

                              You must not call dataChanged() when removing a row.

                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #26

                              @EaccB Sorry, I over read the not

                              This statement is correct.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              0
                              • E EaccB

                                @JonB

                                I think we got out wires crossed. By "update the vector", I meant add and remove elements, as well as editing existing items. This is why I was confused when @Christian-Ehrlicher suggested using dataChanged(), which he had previously told me not to use for removing elements.

                                If I add an element directly to the vector, do I still need to call insertRows() from the model?

                                VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by VRonin
                                #27

                                If you use the model-as-interface-on-existing-data

                                @EaccB said in Custom Table Model:

                                editing existing items

                                • The item that owns the data should emit a signal after the item is modified, the model will connect to this signal and emit dataChanged

                                I meant add and remove elements

                                • The item that owns the data should emit a signal before the item is added/removed, the model will connect to this signal and call beginInsertRows/beginRemoveRows
                                • The item that owns the data should emit a signal after the item is added/removed, the model will connect to this signal and call endInsertRows/endRemoveRows

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                1 Reply Last reply
                                3
                                • artwawA Offline
                                  artwawA Offline
                                  artwaw
                                  wrote on last edited by
                                  #28

                                  Unless the point of your exercise is to create a model - why don't you use built in sqlite for data storage (be it in memory or in a file) and use generic QSqlTableModel, where you don't have to worry about anything? Seems like much less work...

                                  For more information please re-read.

                                  Kind Regards,
                                  Artur

                                  1 Reply Last reply
                                  1

                                  • Login

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