Quantcast
Channel: Installation and Deployment
Viewing all articles
Browse latest Browse all 22112

Drag & drop between two QtreeWidgets

$
0
0
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!

Viewing all articles
Browse latest Browse all 22112

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>