Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Moving a row to another position
Qt 6.11 is out! See what's new in the release blog

Moving a row to another position

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 2.0k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    You can move rows in a QStringListModel with moveRows().
    But what exactly do you want to achieve? Moving the rows by a button click or per drag'n'drop? The latter is already possible without extra work on your side.

    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
    1
    • P Offline
      P Offline
      ppp1
      wrote on last edited by
      #3

      Hi,
      thanks. I want to move the rows per drag and drop. No buttons.

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

        @ppp1 said in Moving a row to another position:

        I want to move the rows per drag and drop.

        Then set dragDropMode of the view to DragDrop and it works.

        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
        1
        • P Offline
          P Offline
          ppp1
          wrote on last edited by
          #5

          Thank. "Drag and drop" was the key. I have activated this mode with

          ui->listView->dragDropMode()
          

          But it doesn't work. Maybe I have to remove the pressed event of the list view. I will try.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            ppp1
            wrote on last edited by ppp1
            #6
                    ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
                    ui->listView->setDragEnabled(true);
                    ui->listView->viewport()->setAcceptDrops(true);
                    ui->listView->setDropIndicatorShown(true);
                    ui->listView->setDragDropMode(QAbstractItemView::InternalMove);
            

            Now it deletes the item at the destination index. I will try later.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              ppp1
              wrote on last edited by
              #7
                      ui->listView->setSelectionMode(QAbstractItemView::SingleSelection);
                      ui->listView->setDragEnabled(true);
                      ui->listView->viewport()->setAcceptDrops(true);
                      ui->listView->setDropIndicatorShown(true);
                      ui->listView->setDragDropMode(QAbstractItemView::InternalMove);
                      ui->listView->setDragDropOverwriteMode(false);
              

              The selected data overwrites the existing item when dropped. The last command does not work. Any hints?

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

                @ppp1 said in Moving a row to another position:

                Any hints?

                It does work for me with a small testcase in the qt designer with QListWidget. What's not needed is the viewport()->setAcceptDrops() call. What Qt version and Os do you use?

                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
                • P Offline
                  P Offline
                  ppp1
                  wrote on last edited by ppp1
                  #9

                  Windows 10 Pro. QT 5.13.1
                  Is it possible to upload you testcase? Or you can test my project? When my testcase works on your system, than I know that something is wrong with my system. Here is my project: Link

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

                    Simply create a new widget ui in designer, add a QTreeWidget, add some items, set the flags and do a preview.

                    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
                    1
                    • P Offline
                      P Offline
                      ppp1
                      wrote on last edited by
                      #11

                      Found the answer: https://www.walletfox.com/course/qtreorderablelist.php. Variant 2.
                      Note: The explanation for why we need to override flags() can be found in the documentation of the method bool dragDropOverwriteMode() const. The documentation says that the default behaviour when moving items is items' removal. To prevent the removal of the items it is necessary to reimplement flags() in such way that we do not return Qt::ItemIsDropEnabled.

                      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
                      • Unsolved