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

    Cheers


  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

    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 ?


  • Qt Champions 2016

    @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(){}
    


  • 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.