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. QTableView move rows and update model
Qt 6.11 is out! See what's new in the release blog

QTableView move rows and update model

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 3.9k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Do you mean you want to be able to change the order of the database fields based on the visual changes you applied to the QTableView ?

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

    1 Reply Last reply
    0
    • blackout69B Offline
      blackout69B Offline
      blackout69
      wrote on last edited by
      #3

      @sgaist said in QTableView move rows and update model:

      Do you mean you want to be able to change the order of the database fields based on the visual changes you applied to the QTableView ?

      Hi,
      Yes. The number field in the table serves as a "work shift". By changing the order in the view, I would like to update the table number field based on the new order. So that by applying a filter to the number field, I can always have the correct order in the view.

      blackout69

      JonBJ VRoninV 2 Replies Last reply
      0
      • blackout69B blackout69

        @sgaist said in QTableView move rows and update model:

        Do you mean you want to be able to change the order of the database fields based on the visual changes you applied to the QTableView ?

        Hi,
        Yes. The number field in the table serves as a "work shift". By changing the order in the view, I would like to update the table number field based on the new order. So that by applying a filter to the number field, I can always have the correct order in the view.

        blackout69

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

        @blackout69
        Since you are reordering via moving headers, you should perhaps read the answer in https://stackoverflow.com/questions/41807590/reading-qtableview-row-contents-with-the-same-order-as-are-seen-not-stored. Or, I think @VRonin's answer at https://forum.qt.io/topic/75393/reading-qtableview-row-contents-with-the-same-order-as-are-seen-not-stored/9 addresses your situation.

        Unless I am missing something (I am interested now), it does seems surprisingly complex to read table view row/columns in visual order instead of model order?

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

          If you don't use the header to hide individual rows, you could try

          QObject::connect(ui->view_relatori->verticalHeader(),&QHeaderView::sectionMoved,[=](int logicalIndex, int oldVisualIndex, int newVisualIndex)->void{
          Q_ASSERT(logicalIndex==oldVisualIndex);
          const QModelIndex numberIndex =model->index(1,logicalIndex); /*I use 1 here because of model->setHeaderData(1, Qt::Horizontal, tr("Number")); it looks strange, however, you have nothing at index 0*/
          Q_ASSERT(logicalIndex==numberIndex.data().toInt());
          model->setData(numberIndex,newVisualIndex);
          });
          

          "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

          blackout69B 1 Reply Last reply
          1
          • JonBJ JonB

            @blackout69
            Since you are reordering via moving headers, you should perhaps read the answer in https://stackoverflow.com/questions/41807590/reading-qtableview-row-contents-with-the-same-order-as-are-seen-not-stored. Or, I think @VRonin's answer at https://forum.qt.io/topic/75393/reading-qtableview-row-contents-with-the-same-order-as-are-seen-not-stored/9 addresses your situation.

            Unless I am missing something (I am interested now), it does seems surprisingly complex to read table view row/columns in visual order instead of model order?

            blackout69B Offline
            blackout69B Offline
            blackout69
            wrote on last edited by
            #6

            @jonb
            reading based on the view would be great, but I don't know how. Could you tell me how?
            blackout69

            1 Reply Last reply
            0
            • VRoninV VRonin

              If you don't use the header to hide individual rows, you could try

              QObject::connect(ui->view_relatori->verticalHeader(),&QHeaderView::sectionMoved,[=](int logicalIndex, int oldVisualIndex, int newVisualIndex)->void{
              Q_ASSERT(logicalIndex==oldVisualIndex);
              const QModelIndex numberIndex =model->index(1,logicalIndex); /*I use 1 here because of model->setHeaderData(1, Qt::Horizontal, tr("Number")); it looks strange, however, you have nothing at index 0*/
              Q_ASSERT(logicalIndex==numberIndex.data().toInt());
              model->setData(numberIndex,newVisualIndex);
              });
              
              blackout69B Offline
              blackout69B Offline
              blackout69
              wrote on last edited by
              #7

              @vronin

              I get this when I move a row

              ASSERT: "logicalIndex==numberIndex.data().toInt()" in file gestione.cpp, line 843
              Invalid parameter passed to C runtime function.

              /I use 1 here because of model->setHeaderData(1, Qt::Horizontal, tr("Number")); it looks strange, however, you have nothing at index 0/
              My index 0 is the table id
              blackout69

              VRoninV 1 Reply Last reply
              0
              • blackout69B blackout69

                @vronin

                I get this when I move a row

                ASSERT: "logicalIndex==numberIndex.data().toInt()" in file gestione.cpp, line 843
                Invalid parameter passed to C runtime function.

                /I use 1 here because of model->setHeaderData(1, Qt::Horizontal, tr("Number")); it looks strange, however, you have nothing at index 0/
                My index 0 is the table id
                blackout69

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

                @blackout69 said in QTableView move rows and update model:

                My index 0 is the table id

                could you qDebug() << logicalIndex <<"!=" << numberIndex.data().toInt(); and post the result?

                "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

                blackout69B 1 Reply Last reply
                0
                • VRoninV VRonin

                  @blackout69 said in QTableView move rows and update model:

                  My index 0 is the table id

                  could you qDebug() << logicalIndex <<"!=" << numberIndex.data().toInt(); and post the result?

                  blackout69B Offline
                  blackout69B Offline
                  blackout69
                  wrote on last edited by
                  #9

                  @VRonin

                  QObject::connect(ui->view_relatori->verticalHeader(),&QHeaderView::sectionMoved,[=](int logicalIndex, int oldVisualIndex, int newVisualIndex)->void{
                  Q_ASSERT(logicalIndex == oldVisualIndex);
                  const QModelIndex numberIndex = relatori_model->index(rel_id_rel,logicalIndex); /*I use 1 here because of model->setHeaderData(1, Qt::Horizontal, tr("Number")); it looks strange, however, you have nothing at index 0*/
                  qDebug() << logicalIndex <<"!=" << numberIndex.data().toInt();
                  Q_ASSERT(logicalIndex == numberIndex.data().toInt());
                  relatori_model->setData(numberIndex,newVisualIndex);
                  });
                  

                  5 != 0
                  ASSERT: "logicalIndex == numberIndex.data().toInt()" in file gestione.cpp, line 844
                  Invalid parameter passed to C runtime function.
                  Invalid parameter passed to C runtime function.

                  blackout69

                  1 Reply Last reply
                  0
                  • blackout69B blackout69

                    @sgaist said in QTableView move rows and update model:

                    Do you mean you want to be able to change the order of the database fields based on the visual changes you applied to the QTableView ?

                    Hi,
                    Yes. The number field in the table serves as a "work shift". By changing the order in the view, I would like to update the table number field based on the new order. So that by applying a filter to the number field, I can always have the correct order in the view.

                    blackout69

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

                    @blackout69 said in QTableView move rows and update model:

                    The number field in the table serves as a "work shift"

                    Looks like it contains a 0 though. what is in that column

                    "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

                    blackout69B 2 Replies Last reply
                    0
                    • VRoninV VRonin

                      @blackout69 said in QTableView move rows and update model:

                      The number field in the table serves as a "work shift"

                      Looks like it contains a 0 though. what is in that column

                      blackout69B Offline
                      blackout69B Offline
                      blackout69
                      wrote on last edited by
                      #11

                      @vronin said in QTableView move rows and update model:

                      Looks like it contains a 0 though. what is in that column

                      In that column there is the table id. So only integers starting from 1
                      The table is thus formed:
                      id_rel int(11)
                      turno decimal(3,1)
                      name varchar(40)
                      email varchar(60)

                      blackout69

                      1 Reply Last reply
                      0
                      • VRoninV VRonin

                        @blackout69 said in QTableView move rows and update model:

                        The number field in the table serves as a "work shift"

                        Looks like it contains a 0 though. what is in that column

                        blackout69B Offline
                        blackout69B Offline
                        blackout69
                        wrote on last edited by
                        #12

                        @vronin
                        Perfect so it works.
                        Thanks for your help.

                              QObject::connect(ui->view_relatori->verticalHeader(), &QHeaderView::sectionMoved, [=](int logicalIndex, int oldVisualIndex, int newVisualIndex)->void{
                              Q_ASSERT(logicalIndex == oldVisualIndex);
                              QModelIndex numberIndex = relatori_model->index(logicalIndex, rel_turno);
                              Q_ASSERT(logicalIndex == numberIndex.data().toInt() - 1);
                              relatori_model->setData(numberIndex, newVisualIndex + 1);
                              relatori_model->submitAll();
                              });
                        

                        blackout69

                        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