PDA

View Full Version : Drag&Drop using QGraphicsScene



topcraft
24th October 2011, 13:46
Hi guys,

I'm trying to implement a little game in which one should be able to drag&drop card items (they inherit from QGraphicsItem) to a QGraphicsScene.

If on the position, where the drag&drop event was placed, there is a CardContainer item (this itself inherits from QGraphicsItem), the drag&drop event shall be transmitted to the CardContainer item, and if not, the QGraphicsScene shall process it (to be able to create a new CardContainer at this position).

I've succeeded in managing the drag&drop between the Cards and the CardContainers, but now I was trying to add the drag&drop for the QGraphicsScene and that's where I'm failing. From what I understood, I need to create my own GraphicsScene to enable drag&drop support, and that's what I did. However by doing so, I unfortunately lost the up-to-then working drag&drop behaviour for the QGraphicsItems. It seems to me, that I'm not correctly propagating the events with Qt's framework, but I have no idea how to do so.


To sum it up: how can I receive the drag&drop events to my QGraphicsItems or, if the event was in the "empty" space of the QGraphicsScene, to my QGraphicsScene?

I'll show you my current code:


#include "dragscene.h"
#include <QtGui>

DragScene::DragScene(qreal x, qreal y, qreal width, qreal height, QWidget *parent) :
QGraphicsScene(x, y, width, height)
{
}

void DragScene::dragEnterEvent(QGraphicsSceneDragDropEv ent *event)
{
if (event->mimeData()->hasColor()) {
event->setAccepted(true);
cout << "DragScene: drag event accepted" << endl; // DEBUG
update();
} else {
event->setAccepted(false);
cout << "DragScene: drag event declined" << endl; // DEBUG
}
}

void DragScene::dragLeaveEvent(QGraphicsSceneDragDropEv ent *event)
{
Q_UNUSED(event);
update();
}

void DragScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
cout << "DragScene: drop event received" << endl; // DEBUG
if (event->mimeData()->hasColor()) {
QColor tmpColor = qVariantValue<QColor>(event->mimeData()->colorData());
QString str = event->mimeData()->text();
// do sth
}
update();
}


What happens right now, is, that when I start a drag&drop action, this directly gets registered by the DragScene. If I then release the mouse button over a CardContainer (QGraphicsItem), the DragScene (!) receives the event (and not the CardContainer). Furthermore, if I release the mouse button in empty space, nothing at all happens (and I think that there's my main problem).

Help is very appreciated!!