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

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Fri Nov 26 16:51:08 CET 2010


On 2010-11-26 ITS-CMT-SUI-SL-SFR-FIN-DEV Knoll Oliver, ITS-CMT-SUI-SL-SFR-FIN-DEV wrote:

> ...
> 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!
> }
> ... and as I read here:
> http://lists.kde.org/?l=kde- devel&m=120895678821473

Ok, so it really seems that one must /not/ call the dragMoveEvent of the super class, but really just:

void MyGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    if (event->mimeData()->hasUrls()) {
        event->accept();
    }
}

Then the corresponding dropEvent() method is also called. I also found an explanation here:

  http://www.qtcentre.org/threads/8022-QGraphicsScene-doesn-t-accept-Drops

"It's because QGraphicsScene provides a default implementation of dragMoveEvent that checks if there are items on the scene under the cursor that accept drop events. If not, the event will be ignored, and you won't be allowed to drop."


Sounds reasonable. But this should be explained in the Qt docs, which left me a bit in the dark: http://doc.trolltech.com/4.7/qgraphicsscene.html#dragEnterEvent even mentions that "The default implementation accepts the event and prepares the scene to accept drag move events.", and http://doc.trolltech.com/4.7/qgraphicsscene.html#dragMoveEvent simply sais "can be reimplemented in a subclass", but doesn't tell anything about the default implementation preventing the drag'n'drop event which had just been accepted previously in the dragEnterMethod().

I think this deserves a chapter in both QGraphicsView/Scene overviews ("caveats for drag'n'drop" or something), with a cross-reference from dragMoveEvent().

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






More information about the Qt-interest-old mailing list