Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved QListWidget drag and drop

    General and Desktop
    3
    8
    5797
    Loading More Posts
    • 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
      GrahamL last edited by SGaist

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • G
          GrahamL @mrjj last edited by

          @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 Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by mrjj

            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 Reply Quote 1
            • mrjj
              mrjj Lifetime Qt Champion last edited by

              @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 Reply Quote 0
              • G
                GrahamL last edited by

                Thanks - I'll give it a go tomorrow

                1 Reply Last reply Reply Quote 0
                • G
                  GrahamL last edited by

                  Thanks

                  All working now

                  1 Reply Last reply Reply Quote 0
                  • O
                    Ostropik last edited by

                    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 Reply Quote 1
                    • First post
                      Last post