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. QListWidget reorder with Drag and Drop
Forum Updated to NodeBB v4.3 + New Features

QListWidget reorder with Drag and Drop

Scheduled Pinned Locked Moved General and Desktop
24 Posts 7 Posters 34.4k 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.
  • S Offline
    S Offline
    szh1
    wrote on last edited by
    #1

    Is there a way to allow a user to reorder QListWidgetItems in a QListWidget by dragging and dropping them, like you can do in Windows Explorer?

    1 Reply Last reply
    1
    • T Offline
      T Offline
      t3chNo
      wrote on last edited by
      #2

      Yes, set listWidget's dragDropMode to QAbstractItemView::DragDrop and defaultDropAction to Qt::MoveAction.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        szh1
        wrote on last edited by
        #3

        I tried this, but when I try to drag an item, it just selects more items.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3chNo
          wrote on last edited by
          #4

          Set selectionMode to QAbstractItemView::SingleSelection.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            szh1
            wrote on last edited by
            #5

            Then, when I try to drag it, it just selects a different row.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              This does the job:

              @
              setDragDropMode(QAbstractItemView::InternalMove);
              @

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              2
              • T Offline
                T Offline
                t3chNo
                wrote on last edited by
                #7

                Set dragEnabled to true.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  szh1
                  wrote on last edited by
                  #8

                  Neither of those are working! :(
                  Am I the only one with this problem?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    t3chNo
                    wrote on last edited by
                    #9

                    Which platform are you working on? Is your qt compiled with QT_NO_DRAGANDDROP defined?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      szh1
                      wrote on last edited by
                      #10

                      I am using Windows 7 with the Qt SDK downloaded from qt.nokia.com/downloads. I did not build it myself.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        This works for me:

                        @
                        #include <QApplication>
                        #include <QListWidget>

                        int main(int argc, char **argv)
                        {
                        QApplication a(argc, argv);

                        QListWidget lw;
                        for(int i = 1; i < 10; ++i)
                            lw.addItem(new QListWidgetItem(QString("Item %1").arg(i)));
                        lw.setDragDropMode(QAbstractItemView::InternalMove);
                        
                        lw.show();
                        
                        a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
                        a.exec&#40;&#41;;
                        

                        }
                        @

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          szh1
                          wrote on last edited by
                          #12

                          Strange. It works for me, too. I wonder why my other application doesn't then.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            Do you use a user interface made with Qt Designer and did you change some of the drag/drop settings there?

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              t3chNo
                              wrote on last edited by
                              #14

                              Do you use QListWidget or QListWidget inherited widget? If you inherit from listWidget and use mouse or drag events and didn't pass event to QListWidget, drag and drop will not work.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                szh1
                                wrote on last edited by
                                #15

                                I made it with Qt Designer and I changed the drag and drop properties a lot, but now I changed them back to just dragDropMode = internalMove and put the others back to default. The selection mode is ExtendedSelection, but I tried the default, too.

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  goetz
                                  wrote on last edited by
                                  #16

                                  That should work. I tried this with designer and could move the items in the preview mode (Ctrl-R/Cmd-R). That should work in the real code too, if you do not change other drag/drop settings there too.

                                  http://www.catb.org/~esr/faqs/smart-questions.html

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    szh1
                                    wrote on last edited by
                                    #17

                                    Even stranger news:

                                    I created a new project, copied and pasted all of the code / widgets, and now its working!

                                    Any ideas why this happened and how to prevent it in the future?

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      goetz
                                      wrote on last edited by
                                      #18

                                      Sorry, I do not have any clue what's happening there. Maybe some alien code from outer space? :-)

                                      http://www.catb.org/~esr/faqs/smart-questions.html

                                      1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        t3chNo
                                        wrote on last edited by
                                        #19

                                        Here is my scenario: You use visual studio. Your code had little error. Complier didn't compile code and ran old executable.

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          szh1
                                          wrote on last edited by
                                          #20

                                          Actually, I use Qt Creator.

                                          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