Drag & drop between two QtreeWidgets



  • Hi,

    I have been working in a quite big project where there was a Qwidget integrated in a QDeclarativeView for a few months, and I had a bug that I am trying to solve. To solve it I am reducing the problem a quite little example.

    My problem is taht I have two QTreeWidgets and I would like to do drag and drop from one to an other (and vice-versa). I am able of drag and dropping QTreeWidgetItems, but, when I drop a QTreeWidgetItem that has children, I lose them and only the parent is dropped.

    I solved this problem by reimplementing the dropEvent and rebuilding the whole tree structure each time, but I am lloking for a better solution because that wasn't efficient and I couldn't implement the Ctrl-Z functionnality for those drag and drops since I was destroying objects all the time... Well, this is what I have:

    @
    #include <QApplication>
    #include <QDebug>
    #include <QObject>
    #include <QWidget>
    #include <QString>
    #include <QTextStream>
    #include <QIODevice>
    #include <QMainWindow>
    #include <QVBoxLayout>

    #include "KTreeWidget.h"
    #include "KAbstractItem.h"
    #include "KItem.h"
    #include "KItemGroup.h"

    int main(int argc, char* argv[]){

    QApplication app(argc, argv);

    QMainWindow window = new QMainWindow();
    QWidget
    central = new QWidget(window);
    QVBoxLayout *layout = new QVBoxLayout();

    KTreeWidget* kv = new KTreeWidget(window);
    KTreeWidget* trash = new KTreeWidget(window);

    layout->addWidget(kv);
    layout->addWidget(trash);

    central->setLayout(layout);

    KItem* kiarr[5];
    for (int i = 0 ; i < 5 ; i++){
    kiarr[i] = new KItem(kv);
    kiarr[i]->setText(0,QString("Item %1").arg(i));
    }
    KItemGroup* kgarr[5];
    for (int i = 0 ; i < 5 ; i++){
    kgarr[i] = new KItemGroup(trash);
    kgarr[i]->setText(0,QString("Group %1").arg(i));
    }

    window->setCentralWidget(central);
    window->show();

    return app.exec();
    }
    @

    My QTreeWidget:
    KtreeWidget.h
    @
    #ifndef KTW_H
    #define KTW_H

    #include <QTreeWidget>

    class KTreeWidget: public QTreeWidget{
    Q_OBJECT

    private:
    QTreeWidgetItem* _header;

    public:
    KTreeWidget(QWidget* w = NULL);
    };

    #endif
    @
    and KTreeWidget.cc:
    @
    #include "KTreeWidget.h"

    KTreeWidget::KTreeWidget(QWidget* w):QTreeWidget(w){
    setColumnCount(3);
    _header = new QTreeWidgetItem(NULL);
    _header->setText(0,"Title");
    _header->setText(1,"Edit");
    _header->setText(2,"Open");
    this->setDefaultDropAction(Qt::MoveAction);
    setHeaderItem(_header);
    setDragEnabled(true);
    setAcceptDrops(true);
    }
    @

    And the items (3 classes, in order to distinguish groups and leafs):
    @
    #ifndef KABSI_H
    #define KABSI_H

    #include <QObject>
    #include <QTreeWidgetItem>
    #include <QTreeWidget>

    class KAbstractItem : public QObject, public QTreeWidgetItem{
    Q_OBJECT
    public:
    KAbstractItem(QTreeWidget* p = NULL);
    };

    #endif
    @
    @
    #include "KAbstractItem.h"
    KAbstractItem::KAbstractItem(QTreeWidget* p):QTreeWidgetItem(p){}
    @
    @
    #ifndef KI_H
    #define KI_H

    #include "KAbstractItem.h"

    class KItem : public KAbstractItem{
    Q_OBJECT
    public:
    KItem(QTreeWidget* p);
    };

    #endif
    @
    @
    #include "KItem.h"

    KItem::KItem(QTreeWidget* p):KAbstractItem(p){
    setFlags(Qt::ItemIsSelectable
    | Qt::ItemIsEditable
    | Qt::ItemIsDragEnabled
    | Qt::ItemIsUserCheckable
    | Qt::ItemIsEnabled);
    }
    @

    @
    #ifndef KIG_H
    #define KIG_H

    #include "KAbstractItem.h"

    class KItemGroup : public KAbstractItem{
    Q_OBJECT
    public:
    KItemGroup(QTreeWidget* p);
    };

    #endif
    @
    @
    #include "KItemGroup.h"

    KItemGroup::KItemGroup(QTreeWidget* p):KAbstractItem(p){
    setFlags(Qt::ItemIsSelectable
    | Qt::ItemIsEditable
    | Qt::ItemIsDragEnabled
    | Qt::ItemIsDropEnabled
    | Qt::ItemIsUserCheckable
    | Qt::ItemIsEnabled);
    }
    @

    Whenever I do a drop of one of the Items in the first WTreeWifget inside one of the groups of the second one, it works, but it then I move a group to the top QTreeWidget, I lose all the children...

    Could you tell me what I am doing wrong?
    Thanks a lot!


Log in to reply
 

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