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. Change order of rows in a QTableWidet by drag & drop
QtWS25 Last Chance

Change order of rows in a QTableWidet by drag & drop

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 9.8k 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.
  • D Offline
    D Offline
    Daniel T
    wrote on 14 Nov 2015, 21:03 last edited by
    #1

    Hi my friends.
    I want to ask if it possible to make a QTableWidget which permit to drag whole rows, for only change the order of rows.
    It is necessary to subclass QTableWidget ? And how ? Have we some example in C++ ?
    (I use Qt 5.5)

    greatings, Daniel

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Nov 2015, 21:27 last edited by
      #2

      Hi,

      IIRC, you just need to enabled Drag'n'Drop on your QTableWidget, set the selection mode to Row and the DradDropMode to InternalMove and you should be good to go.

      Hope it helps

      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
      • D Offline
        D Offline
        Daniel T
        wrote on 14 Nov 2015, 22:18 last edited by
        #3

        Thanks for your answer.
        I tried
        m_tagsTable->setDragEnabled(true);
        m_tagsTable->setDragDropMode(QAbstractItemView::InternalMove);
        m_tagsTable->verticalHeader()->setSectionsMovable(true);
        but, if a have {1,2,3} or order of rows at beginning and drag row 1 to 3, then i have { nothing, 2,1} but not { 2,3,1}
        Can you help me then ?
        Daniel

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Nov 2015, 22:20 last edited by
          #4

          You're missing m_tagsTable->setAcceptDrops(true);

          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
          • D Offline
            D Offline
            Daniel T
            wrote on 14 Nov 2015, 22:28 last edited by
            #5

            Thanks, but it also not work :
            m_tagsTable->setDragEnabled(true);
            m_tagsTable->setDragDropMode(QAbstractItemView::InternalMove);
            m_tagsTable->verticalHeader()->setSectionsMovable(true);
            m_tagsTable->setAcceptDrops(true);
            From {1,2,3} it become { nothing,2,1} (the origin row is deleted, without content)
            Know you some other trick ?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 14 Nov 2015, 22:35 last edited by
              #6

              Sorry, my bad, using m_tagsTable->verticalHeader()->setSectionsMovable(true); should be enough unless you want to move the rows from selection.

              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
              • D Offline
                D Offline
                Daniel T
                wrote on 14 Nov 2015, 22:44 last edited by
                #7

                So, you know some way for reorder a QTableWidget ?
                In a QListWidget, its easy to do, but with this instructions , which i show in last message, dont work for reorder.
                This only move a row to a destination, but leave origin row in blanc.
                Daniel

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 14 Nov 2015, 23:10 last edited by
                  #8

                  Sorry, I wasn't clear in my last message. For QTableWidget and unless you want to move your rows based on selection (which would be unusual) you can remove the D'n'D stuff and only use setSectionsMovable on the vertical header.

                  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
                  • D Offline
                    D Offline
                    Daniel T
                    wrote on 14 Nov 2015, 23:22 last edited by
                    #9

                    I already understand this, but you not answer my answer.
                    May be i am not clear ?
                    I sent you a website in which already somebody ask this (for a previous Qt version) and receive a answer based on PyQt, but i dont understand well PyQt :
                    http://stackoverflow.com/questions/26227885/drag-and-drop-rows-within-qtablewidget/26311179#26311179
                    Use "setSectionsMovable" with or without D'n'D stuff, dont work.
                    Thanks for you pacience.
                    Daniel

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 14 Nov 2015, 23:32 last edited by
                      #10

                      Before digging too deep into the D'n'D part
                      Does:

                      int main(int argc, char *argv[]) 
                      {
                          QApplication app(argc, argv);
                          QTableWidget tw;
                          tw.setRowCount(3);
                          tw.setColumnCount(3);
                      
                          for (int i = 0 ; i < tw.rowCount() ; ++i) {
                              for (int j = 0 ; j < tw.colorCount() ; ++j) {
                                  QTableWidgetItem *item = new QTableWidgetItem(QString("%1 %2").arg(i).arg(j));
                                  tw.setItem(i, j, item);
                              }
                          }
                      
                          tw.verticalHeader()->setSectionsMovable(true);
                          tw.show();
                          return app.exec();
                      }
                      

                      do what you want ?
                      Note that when setting setSectionsMovable, you have to use the section "button" to move a row.

                      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
                      • D Offline
                        D Offline
                        Daniel T
                        wrote on 14 Nov 2015, 23:57 last edited by Daniel T
                        #11

                        Thanks.
                        I already compiled this code i tryed it.
                        If i drag from (1,1) (which contains "0 0") and drag to (3,1), it simply select (1,1),(2,1) and (3,1)
                        I show you this result :
                        image
                        Sorry i dont now how to "incrust" a image here in this chat, so i give a link.
                        What i want to have as a result after dragging from "0 0" to "2 0" is :
                        "1 0" "1 1" "1 2"
                        "2 0" "2 1" "2 2"
                        "0 0" "0 1" "0 2"
                        So its like "insert".
                        Thanks any more help. Daniel

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 15 Nov 2015, 00:02 last edited by
                          #12

                          Using setSectionsMovable, you don't select anything. Just put your mouse on the section number, press and move the section (just like you would do with e.g. Excel or Numbers)

                          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
                          • D Offline
                            D Offline
                            Daniel T
                            wrote on 15 Nov 2015, 00:08 last edited by
                            #13

                            Of course this is what do :
                            Make a click over "0,0". This will select "0,0". I drag my mouse until i am over "2 0", release, and i already show you which result i get.
                            No reorder, no insertion , only selection from "0 0" to "2 0"
                            Daniel

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              Daniel T
                              wrote on 15 Nov 2015, 00:20 last edited by
                              #14

                              Hi.
                              I found a way.
                              However dragging a cell not work, its work if a drag a vertical header.
                              Of course it would good if i can drag cells of column 0, but i am already satisfied for this moment.
                              Thanks. Daniel

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 15 Nov 2015, 00:21 last edited by
                                #15

                                You're doing the opposite of what I ask you to try. By the way, if you want to move the columns you need to set the section movable property on the horizontal header (I know, this one is a bit confusing)

                                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
                                • D Offline
                                  D Offline
                                  Daniel T
                                  wrote on 15 Nov 2015, 00:51 last edited by
                                  #16

                                  What i move are items of the header which is at left. I called it QVertcalHeader.
                                  I am wrong ? Anyway, setting this QVertcalHeader to movable, permit me to move rows (moveing items on this header)
                                  I also search for get a answer, which one is a QVerticalHeader, but for now this is for me more raisonable.
                                  I only know to which say a "row header" at : https://documentation.devexpress.com/#WindowsForms/CustomDocument387
                                  greatings, Daniel

                                  1 Reply Last reply
                                  0
                                  • Y Offline
                                    Y Offline
                                    yeckel
                                    wrote on 15 Nov 2015, 17:13 last edited by
                                    #17

                                    Hi Daniel, there was a bugQTBUG-48408 is it the same problem? Should be solved in Qt5.6.

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 15 Nov 2015, 20:10 last edited by
                                      #18

                                      @yeckel nop, that's not the same thing. That bug's about the selection model + sorting. Here the subject is moving a row up/down.

                                      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

                                      10/18

                                      14 Nov 2015, 23:32

                                      • Login

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