Qt drag and drop between two QListWidget

后端 未结 1 1423
礼貌的吻别
礼貌的吻别 2020-12-18 23:24

I\'ve two QListWidget (list1 and list2)

  • list1 should be able to receive items from list2
  • list1 should be able t
相关标签:
1条回答
  • 2020-12-19 00:03

    You can extend QListWidget overriding dragMoveEvent method like below

    #ifndef MYLISTWIDGET_HPP
    #define MYLISTWIDGET_HPP
    
    #include <QListWidget>
    
    class MyListWidget : public QListWidget {
    
    public:
        MyListWidget(QWidget * parent) :
            QListWidget(parent) {}
    
    protected:
        void dragMoveEvent(QDragMoveEvent *e) {
            if (e->source() != this) {
                e->accept();
            } else {
                e->ignore();
            }
        }
    };
    
    #endif // MYLISTWIDGET_HPP
    

    Inside our implementation we check the source of the drag event and we don't accept (allow) dropping items that come from our widget itself.
    If you're using QtDesigner you can use Promote to... option from the context menu when you right click on the QListWidget on your form. You have to enter a name of your new class (MyListWidget in my example) and you have to enter a name of new header file, where your class will be declared (you can copy and paste the code above into this file).

    0 讨论(0)
提交回复
热议问题