How to Drag and Drop QTreeWidgetItem into a QGraphicsView
-
Hi, i need to do something like WidgetBox (QDesigner).
I use a QTreeWidget with QTreeWidgetItems. This items has an object that inherits from QGrahicsItem. I want to drag this item into a QGraphicsView to draw that.
I was looking for information about QMimeData, QDataStream, QBinaryArray and about QMimeType but i don't understand how they work. Can someone help me? I'm desperate ;(
The code maybe has errors:
ItemBlock.h
#ifndef ITEMBLOCK_H #define ITEMBLOCK_H #include <QTreeWidgetItem> #include "block.h" #include <QMimeData> class ItemBlock : public QTreeWidgetItem { public: ItemBlock(const QString &_nombre, QTreeWidgetItem *_categoria); Block* getBlock() const { return bloque; } private: Block *bloque; }; #endif // ITEMBLOCK_H
Block.h
#ifndef BLOCK_H #define BLOCK_H #include <QGraphicsItem> class Block : public QGraphicsItem { public: Block(const QString &_nombre ); QString getNombre() const; QRectF boundingRect() const Q_DECL_OVERRIDE; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE; protected: void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; // void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; // void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; private: QString nombre; }; #endif // BLOCK_H
-
Hi,
Rather than having your Block in the QTreeWidgetItem, why not provide the information you need to create one Block in the mime data and let your view create the block as needed ?
-
@SGaist Thanks, it's a nice idea, i'll try it, but now i have a doubt: Can QMimeData to contain pointers to objects or contains Strings only? i was taking a look to subclass QMimeData but it's hard to me. Sorry, i'm newbie in Qt, i have to do the final project in Qt and i'm trying to understand this concepts.
-
QMimeData can contain what you need, it just optimized for the common cases. However, to add some more weight to my suggestion: if you give your scene the Block contained in ItemBlock, what will happen ? The scene takes ownership of the item you put it.
-
@Brikinhos Hi Brikinhos, do you mint to share what where your solution?
Thanks.