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:

Qt Code:
  1. #include "dragscene.h"
  2. #include <QtGui>
  3.  
  4. DragScene::DragScene(qreal x, qreal y, qreal width, qreal height, QWidget *parent) :
  5. QGraphicsScene(x, y, width, height)
  6. {
  7. }
  8.  
  9. void DragScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
  10. {
  11. if (event->mimeData()->hasColor()) {
  12. event->setAccepted(true);
  13. cout << "DragScene: drag event accepted" << endl; // DEBUG
  14. update();
  15. } else {
  16. event->setAccepted(false);
  17. cout << "DragScene: drag event declined" << endl; // DEBUG
  18. }
  19. }
  20.  
  21. void DragScene::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
  22. {
  23. Q_UNUSED(event);
  24. update();
  25. }
  26.  
  27. void DragScene::dropEvent(QGraphicsSceneDragDropEvent *event)
  28. {
  29. cout << "DragScene: drop event received" << endl; // DEBUG
  30. if (event->mimeData()->hasColor()) {
  31. QColor tmpColor = qVariantValue<QColor>(event->mimeData()->colorData());
  32. QString str = event->mimeData()->text();
  33. // do sth
  34. }
  35. update();
  36. }
To copy to clipboard, switch view to plain text mode 

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