PDA

View Full Version : <Solved> How to handel Drag Events of Child QGraphicItems



0backbone0
20th August 2015, 18:39
Hi,

recently I started to write a Node Editor.
Right now I just use custom QGraphicsItems inside a standard scene.
The Body of the Nodes use ItemIsMoveable, the Pins are Child Items derived from QGraphicsItem.

The Pins.h file:


#ifndef NODEINPUTPLUG_H
#define NODEINPUTPLUG_H

#include <QGraphicsItem>
#include <QPainter>
#include <QMouseEvent>
#include <QDrag>

class nodeinputplug : public QGraphicsItem
{
public:
nodeinputplug();

QRectF boundingRect() const;

void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget);

QDrag *drag;

protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
};

#endif // NODEINPUTPLUG_H


To check if the MouseEvents work i just put a qDebug() << event; in every of the Mouse handling functions.
Everything works well except dragLeaveEvent.
I tried to invoke draging like in the Drag and Drop Robot example. However if I try to create a drag object
inside the mouseMoveEvent function I just get this Error Message:


no matching function for call to 'QDrag::QDrag(nodeinputplug* const)'
QDrag *drag = new QDrag(this);

I also tried to create a drag object inside the constructor, but this gives me roughly the same error.
Do I have to derive from QObject also?

anda_skoa
20th August 2015, 21:20
You could just pass the scene as the drag's parent.

Cheers,
_

0backbone0
21st August 2015, 00:22
Thank you for your answer.

Unfortunately passing the Scene didn't work. I found another Solution thou.
After I included QWidget I could pass the Widget of the event as argument.
This still didn't invoke a drag action because the drag->exec() was missing.



QDrag *drag = new QDrag(event->widget());
QMimeData *mimeData = new QMimeData;
drag->setMimeData(mimeData);

Qt::DropAction dropAction = drag->exec();