Nominate our 2022 Qt Champions!

QListWidget drag and drop

  • 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


  • Lifetime Qt Champion

    have a look at this

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

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


  • Lifetime Qt Champion

    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 ?

  • Lifetime Qt Champion

    @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.


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


    void myhappylist::dragEnterEvent ( QDragEnterEvent *event )
    	if ( event->source() != this ) {		
    		setDragDropMode ( QAbstractItemView:: DragDrop );
    	} else {
    		setDragDropMode ( QAbstractItemView::InternalMove );

  • Thanks - I'll give it a go tomorrow

  • Thanks

    All working now

  • 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 )" ))

Log in to reply