My problem is that 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 don't really see how to do it. The only way I have found is reimplementing dropEvent and destroying all teh dropped objects and reconsructing them .. but I don't like that solution because it's slow and the objets are not the same, so it complicates very much the implementation of a Undo/redo feature...
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):
KAbstractItem.h
#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
and KAbstractItem.cc
#include "KAbstractItem.h"
KAbstractItem::KAbstractItem(QTreeWidget* p):QTreeWidgetItem(p){}
KItem.h
#ifndef _KI_H_
#define _KI_H_
#include "KAbstractItem.h"
class KItem : public KAbstractItem{
Q_OBJECT
public:
KItem(QTreeWidget* p);
};
#endif
and KItem.cc
#include "KItem.h"
KItem::KItem(QTreeWidget* p):KAbstractItem(p){
setFlags(Qt::ItemIsSelectable
| Qt::ItemIsEditable
| Qt::ItemIsDragEnabled
| Qt::ItemIsUserCheckable
| Qt::ItemIsEnabled);
}
and KItemGroup.h
#ifndef _KIG_H_
#define _KIG_H_
#include "KAbstractItem.h"
class KItemGroup : public KAbstractItem{
Q_OBJECT
public:
KItemGroup(QTreeWidget* p);
};
#endif
and KItemGroup.h
#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 in advance!