[Qt-interest] QGraphicsScene: drag'n'drop onto empty scene? (dropEvent() never called)

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Fri Nov 26 16:37:43 CET 2010


Hi,

I want to be able to drag image file paths onto my QGraphicsScene. On the corresponding QGraphicsView I call setAcceptDrops(true). The QGraphicsScene is empty (I want to add images with drag'n'drop).

It is a stock QGraphicsView added to my QMainWindow in Qt Designer - for now I try avoiding inheriting from this QGraphicsView, as to keep the design in Qt Creator simple and not having to specify relative #include paths within Designer to custom widgets.

In my derived QGraphicsScene I re-implement:

void MyGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
  if (event->mimeData()->hasUrls()) {
    event->accept(); // is indeed called in debugger
  }
} 

void MyGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    QGraphicsScene::dragMoveEvent(event);
    event->accept();  // this code is also called - see comment below
}

void MyGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    ...; // this method is never called!
}

So dragEnterEvent is called as expected, but the cursor never changes shape to "accepted". And what's more, dropEvent() is never called.

I tried the "workaround" to accept() the event also within dragMoveEvent, as suggested by Mihail [see "QgraphicsScene: best way to manage events" currently being discussed as well], and as I read here: http://lists.kde.org/?l=kde-devel&m=120895678821473

But apparently this does not seem to work (anymore?) with Qt 4.7.1 on Windows XP (binary distribution, MinGW).


I modified the Qt example graphicsview/dragdroprobot, so that /all/ events are accepted (in dragEnterEvent). And indeed, that seems to work, my file image path is accepted, the cursor changes shape and the dropEvent method is called! But these methods are implemented on the QGraphicsItem (the "robot"), and not on the QGraphicsScene...


Or am I still doing something fundamentally wrong? I will try to inherit from QGraphicsView instead and see whether that makes a difference. But the Qt docs gave me the impression that it should really work like this, so re-implement the drag'n'drop event methods in QGraphicsSCENE only. And the fact that dragEnterEvent (and move) is really called tells me that I am at least half-way right with this assumption...

Thanks,
  Oliver
-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22






More information about the Qt-interest-old mailing list