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. Reimplementation of QAbstractItemModel::setData
Forum Update on Monday, May 27th 2025

Reimplementation of QAbstractItemModel::setData

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 4.3k Views
  • 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.
  • ZoptuneZ Offline
    ZoptuneZ Offline
    Zoptune
    wrote on last edited by
    #1

    Hi,

    I have a QTableView that display the content of a QSqlQueryModel. But now i need to merge some rows.
    I decided to merge the content of the rows but i can't modify the QSqlQueryModel.

    I use a custom QSortFilterProxyModel.
    But I don't know how to reimplement the setData function. I also need to reimplement the data function to get the content to merge.

    Thx

    VRoninV 1 Reply Last reply
    0
    • ZoptuneZ Zoptune

      Hi,

      I have a QTableView that display the content of a QSqlQueryModel. But now i need to merge some rows.
      I decided to merge the content of the rows but i can't modify the QSqlQueryModel.

      I use a custom QSortFilterProxyModel.
      But I don't know how to reimplement the setData function. I also need to reimplement the data function to get the content to merge.

      Thx

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

      @Zoptune said in Reimplementation of QAbstractItemModel::setData:

      merge some rows

      error C2065: 'merge some rows' : undeclared identifier

      Please define what you mean

      "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

      ZoptuneZ 1 Reply Last reply
      2
      • VRoninV VRonin

        @Zoptune said in Reimplementation of QAbstractItemModel::setData:

        merge some rows

        error C2065: 'merge some rows' : undeclared identifier

        Please define what you mean

        ZoptuneZ Offline
        ZoptuneZ Offline
        Zoptune
        wrote on last edited by
        #3

        Hi @VRonin ,

        I have rows in my TableView that represent real objects. Special objects can be created in pair. This is why i want to merge rows.
        These special object have the same name but not the same length.

        Example :
        At the begining i have two row with name XXXXX and length 5m and 10m.
        At the end, i want to have one row with name XXXXX and length 5m + 10m.

        I hope I was clear in my explanations.

        Thx

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Would you like the data in your original model to be kept separate or merged?

          Are name and length 2 different columns?

          "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

          ZoptuneZ 1 Reply Last reply
          1
          • VRoninV VRonin

            Would you like the data in your original model to be kept separate or merged?

            Are name and length 2 different columns?

            ZoptuneZ Offline
            ZoptuneZ Offline
            Zoptune
            wrote on last edited by
            #5

            @VRonin

            I don't care if the data is merged or not in the original model and yes name and length are separated columns.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Last question, I promise: does the model need to editable?

              "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

              ZoptuneZ 1 Reply Last reply
              0
              • VRoninV VRonin

                Last question, I promise: does the model need to editable?

                ZoptuneZ Offline
                ZoptuneZ Offline
                Zoptune
                wrote on last edited by
                #7

                @VRonin

                If you ask me if the user can edit the model, the answer is no. I just want to display the data but i need to merge some of them before.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  implemented here: http://pastebin.com/K3Xmquyz

                  example usage:

                  #include <QApplication>
                  #include <QStandardItemModel>
                  #include <QTableView>
                  #include <QHBoxLayout>
                  #include "mergeproxy.h"
                  int main(int argc, char **argv)
                  {
                  
                      QApplication app(argc,argv);
                      QStandardItemModel baseModel;
                      baseModel.insertRows(0, 5);
                      baseModel.insertColumns(0, 2);
                      baseModel.setHeaderData(0, Qt::Horizontal, "Col1");
                      baseModel.setHeaderData(1, Qt::Horizontal, "Col2");
                      baseModel.setData(baseModel.index(0, 0), "Test");
                      baseModel.setData(baseModel.index(0, 1), 1);
                      baseModel.setData(baseModel.index(1, 0), "Test");
                      baseModel.setData(baseModel.index(1, 1), 2);
                      baseModel.setData(baseModel.index(2, 0), "Test2");
                      baseModel.setData(baseModel.index(2, 1), 3);
                      baseModel.setData(baseModel.index(3, 0), "Test2");
                      baseModel.setData(baseModel.index(3, 1), 4);
                      baseModel.setData(baseModel.index(4, 0), "Alone");
                      baseModel.setData(baseModel.index(4, 1), 100);
                  
                      MergeProxy baseProxy;
                      baseProxy.setSourceModel(&baseModel);
                      baseProxy.setMergeKeyColumn(0);
                  
                      QWidget mainWidget;
                      QTableView* savedView=new QTableView(&mainWidget);
                      savedView->setModel(&baseModel);
                      QTableView* loadedView=new QTableView(&mainWidget);
                      loadedView->setModel(&baseProxy);
                      QHBoxLayout* mainLay = new QHBoxLayout(&mainWidget);
                      mainLay->addWidget(savedView);
                      mainLay->addWidget(loadedView);
                      mainWidget.show();
                      return app.exec();
                  }
                  

                  "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

                  ZoptuneZ 2 Replies Last reply
                  2
                  • VRoninV VRonin

                    implemented here: http://pastebin.com/K3Xmquyz

                    example usage:

                    #include <QApplication>
                    #include <QStandardItemModel>
                    #include <QTableView>
                    #include <QHBoxLayout>
                    #include "mergeproxy.h"
                    int main(int argc, char **argv)
                    {
                    
                        QApplication app(argc,argv);
                        QStandardItemModel baseModel;
                        baseModel.insertRows(0, 5);
                        baseModel.insertColumns(0, 2);
                        baseModel.setHeaderData(0, Qt::Horizontal, "Col1");
                        baseModel.setHeaderData(1, Qt::Horizontal, "Col2");
                        baseModel.setData(baseModel.index(0, 0), "Test");
                        baseModel.setData(baseModel.index(0, 1), 1);
                        baseModel.setData(baseModel.index(1, 0), "Test");
                        baseModel.setData(baseModel.index(1, 1), 2);
                        baseModel.setData(baseModel.index(2, 0), "Test2");
                        baseModel.setData(baseModel.index(2, 1), 3);
                        baseModel.setData(baseModel.index(3, 0), "Test2");
                        baseModel.setData(baseModel.index(3, 1), 4);
                        baseModel.setData(baseModel.index(4, 0), "Alone");
                        baseModel.setData(baseModel.index(4, 1), 100);
                    
                        MergeProxy baseProxy;
                        baseProxy.setSourceModel(&baseModel);
                        baseProxy.setMergeKeyColumn(0);
                    
                        QWidget mainWidget;
                        QTableView* savedView=new QTableView(&mainWidget);
                        savedView->setModel(&baseModel);
                        QTableView* loadedView=new QTableView(&mainWidget);
                        loadedView->setModel(&baseProxy);
                        QHBoxLayout* mainLay = new QHBoxLayout(&mainWidget);
                        mainLay->addWidget(savedView);
                        mainLay->addWidget(loadedView);
                        mainWidget.show();
                        return app.exec();
                    }
                    
                    ZoptuneZ Offline
                    ZoptuneZ Offline
                    Zoptune
                    wrote on last edited by
                    #9

                    @VRonin

                    Wow there's a lot of things i don't understand or that i have never used ^^

                    Thank you very much it seems to be what i want !
                    I need to work and understand now :)

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      implemented here: http://pastebin.com/K3Xmquyz

                      example usage:

                      #include <QApplication>
                      #include <QStandardItemModel>
                      #include <QTableView>
                      #include <QHBoxLayout>
                      #include "mergeproxy.h"
                      int main(int argc, char **argv)
                      {
                      
                          QApplication app(argc,argv);
                          QStandardItemModel baseModel;
                          baseModel.insertRows(0, 5);
                          baseModel.insertColumns(0, 2);
                          baseModel.setHeaderData(0, Qt::Horizontal, "Col1");
                          baseModel.setHeaderData(1, Qt::Horizontal, "Col2");
                          baseModel.setData(baseModel.index(0, 0), "Test");
                          baseModel.setData(baseModel.index(0, 1), 1);
                          baseModel.setData(baseModel.index(1, 0), "Test");
                          baseModel.setData(baseModel.index(1, 1), 2);
                          baseModel.setData(baseModel.index(2, 0), "Test2");
                          baseModel.setData(baseModel.index(2, 1), 3);
                          baseModel.setData(baseModel.index(3, 0), "Test2");
                          baseModel.setData(baseModel.index(3, 1), 4);
                          baseModel.setData(baseModel.index(4, 0), "Alone");
                          baseModel.setData(baseModel.index(4, 1), 100);
                      
                          MergeProxy baseProxy;
                          baseProxy.setSourceModel(&baseModel);
                          baseProxy.setMergeKeyColumn(0);
                      
                          QWidget mainWidget;
                          QTableView* savedView=new QTableView(&mainWidget);
                          savedView->setModel(&baseModel);
                          QTableView* loadedView=new QTableView(&mainWidget);
                          loadedView->setModel(&baseProxy);
                          QHBoxLayout* mainLay = new QHBoxLayout(&mainWidget);
                          mainLay->addWidget(savedView);
                          mainLay->addWidget(loadedView);
                          mainWidget.show();
                          return app.exec();
                      }
                      
                      ZoptuneZ Offline
                      ZoptuneZ Offline
                      Zoptune
                      wrote on last edited by
                      #10

                      @VRonin

                      Hi, it's me again.

                      I not sure i understood, in what function do you change the data ?

                      From what I understood, this is the MergeProxy::data function that edit the data.

                      Because now i'm working with a bigger table and more complex treatment.

                      I need to merge rows using two mergeKeyColumn.

                      Example :

                      alt text

                      Here the two last columns are the two mergeKeyColumn (first one is "pair_id" and second one is "id")
                      And "AKSA" from row 2 col 3 need to go at col 4.

                      Result :
                      alt text

                      Thx

                      1 Reply Last reply
                      0
                      • ZoptuneZ Offline
                        ZoptuneZ Offline
                        Zoptune
                        wrote on last edited by
                        #11

                        Please i need help ^^

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          What you need is 100% data dependant, there is no way of implementing it in a general way.
                          You are better off just executing the query manually and fill a QStandardItemModel manually meging the cells when necessary instead of using QSqlQueryModel

                          "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
                          1
                          • ZoptuneZ Offline
                            ZoptuneZ Offline
                            Zoptune
                            wrote on last edited by
                            #13

                            Ok, i thought was an another way to merge rows.

                            Thank you

                            1 Reply Last reply
                            0

                            • Login

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