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 drag and drop
Forum Updated to NodeBB v4.3 + New Features

QListWidget drag and drop

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 8.6k Views 2 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.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by SGaist
    #1

    Hi
    I have a QListWidget that has drag and drop enabled and I am able to drag an item onto it (from another widget) and have it added to the list
    My problem is that if I drag an item within the list the item being dragged replaces the item underneath, whereas I want to just change the order.

    Has anyone got any ideas as to what is happening here

    Cheers

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      have a look at this
      https://forum.qt.io/topic/1786/qlistwidget-reorder-with-drag-and-drop/3

      I think setDragDropMode(QAbstractItemView::InternalMove); is the key.

      G 1 Reply Last reply
      0
      • mrjjM mrjj

        have a look at this
        https://forum.qt.io/topic/1786/qlistwidget-reorder-with-drag-and-drop/3

        I think setDragDropMode(QAbstractItemView::InternalMove); is the key.

        G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

        @mrjj
        Hi Thanks for your reply
        I did try setting the mode to internal move but then I cant drag from from an external widget

        Cheers

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          oh, well the name kinda suggested that now you tell me ;)

          Sorry, did never try to mix both internal and external drop. But I do understand why you want it.
          Maybe you can handle it yourself ?

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @mrjj said:
            This class allows to rearrange internally and drop from other list by
            changing setDragDropMode on the fly.
            For easy use , use the promote feature.
            Sorry for the lame name.

            myhappylist.h

            #ifndef MYHAPPYLIST_H
            #define MYHAPPYLIST_H
            #include <QListWidget>
            #include <QDragEnterEvent>
            
            class myhappylist : public QListWidget
            {
            public:
                explicit myhappylist(QWidget *parent = 0) : QListWidget(parent) { }
             
                ~myhappylist();
            protected:
                void dragEnterEvent(QDragEnterEvent *event);
            };
            

            myhappylist.cpp

            void myhappylist::dragEnterEvent ( QDragEnterEvent *event )
            {
            	if ( event->source() != this ) {		
            		setDragDropMode ( QAbstractItemView:: DragDrop );
                           event->accept();
            	} else {
            		setDragDropMode ( QAbstractItemView::InternalMove );
            		event->accept();
            
            	}
            
            }
            myhappylist::~myhappylist(){}
            
            1 Reply Last reply
            0
            • G Offline
              G Offline
              GrahamL
              wrote on last edited by
              #6

              Thanks - I'll give it a go tomorrow

              1 Reply Last reply
              0
              • G Offline
                G Offline
                GrahamL
                wrote on last edited by
                #7

                Thanks

                All working now

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  Ostropik
                  wrote on last edited by
                  #8

                  I have the same issue. When i make custom class
                  Solved by implementing all D&D parent events. (But in any topic two enough)

                  DndListWidget : public QListWidget
                  {
                      void dragEnterEvent(QDragEnterEvent *event) override;
                      void dropEvent(QDropEvent *event) override;
                      // if this two will not be implemented DnD will works not properly
                      void dragMoveEvent(QDragMoveEvent* event) override;
                      void startDrag(Qt::DropActions supportedActions)  override; 
                  }
                  

                  @mrjj Thanks for "if ( event->source() != this )" ))

                  1 Reply Last reply
                  1

                  • Login

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