January 12, 2011

Olorin5800 Olorin5800
Lab Rat
39 posts

QGraphicsScene drag and drop

 

I have a QGraphicsScene based class that accepts drops and everything works ok.
But QGraphicsWidget derived classes i have do not accept drops.

  1. void Scene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
  2. {
  3. //here
  4.     SourceWidget *source = qobject_cast<SourceWidget *>(event->source());
  5.     if(source) {
  6.         event->setDropAction(Qt::CopyAction);
  7.         event->accept();
  8.     }
  9. }
  10.  
  11. void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
  12. {
  13.     SourceWidget *source = qobject_cast<SourceWidget *>(event->source());
  14.     if(source) {
  15.         event->setDropAction(Qt::CopyAction);
  16.         event->accept();
  17.     }
  18.  
  19. }
  20.  
  21. void Scene::dropEvent(QGraphicsSceneDragDropEvent *event)
  22. {
  23.     SourceWidget *source = qobject_cast<SourceWidget *>(event->source());
  24.     if(source) {
  25.         createWidget(static_cast<Widget::WidgetType>(event->mimeData()->text().toInt()), event->scenePos());
  26.         event->setDropAction(Qt::CopyAction);
  27.         event->accept();
  28.     }
  29. }

this works super but when using the same code in a qgraphicswidget based class ( with accept drops set to true)
the code dose not even reach to the point of the //here comment when removing the 3 handlers from the scene class the items accept drops OK.
I’m just curios if the scene accepts drops items can’t?

 Signature 

—-
My programs never have bugs, they just develop random features.

10 replies

January 13, 2011

Andre Andre
Robot Herder
6295 posts

Reimplementing virtual methods is always tricky. I think your problem is, is that your Scene implementation doesn’t call the base class implementation of the virtual methods you reimplemented. This way, you disabled Qt’s own event propagation mechanisms that are implemented in the base classes.

Unless you’re sure you should not, always call the base class implementation in your reimplementation of a virtual class. It may do actual work you were not aware of.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

January 13, 2011

Olorin5800 Olorin5800
Lab Rat
39 posts

Thanks for the answer I thought that to.

 Signature 

—-
My programs never have bugs, they just develop random features.

January 13, 2011

Volker Volker
Ant Farmer
5428 posts

What is the error on //here?
Is the method called at all?
Is it called but you do not get into accepting the event?

January 13, 2011

Andre Andre
Robot Herder
6295 posts

Olorin5800 wrote:
Thanks for the answer I thought that to.

But…? That doesn’t solve your issue?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

January 13, 2011

Olorin5800 Olorin5800
Lab Rat
39 posts

Andre wrote:

Olorin5800 wrote:
Thanks for the answer I thought that to.

But…? That doesn’t solve your issue?

Can’t test right now but looking through the qt source code not calling the base implementation seems to be the problem in the qgraphicsscene implementation of the drag move event is where the forwarding is taking place my code overrides the event and never calls the parent.

Volker wrote:
What is the error on //here? Is the method called at all? Is it called but you do not get into accepting the event?

No error the method where t i put the comment is not called in the item class if overriding the scene implementation of drag enter, drag move and drop events.

 Signature 

—-
My programs never have bugs, they just develop random features.

January 13, 2011

Volker Volker
Ant Farmer
5428 posts

gnarf … of course it does not call the dragEnterEvent method of your item if the scene already accepts the drag!

If you eventually drop your drag – there is only one object to receive it – it’s that one, that accepted the dragEnter/dragMove events.

Hypothetically, if you allowed both to dragEnter, which one should get the drop at the end? Both? The top one (on z-order)? The bottom one? Rolling dices?

January 13, 2011

Olorin5800 Olorin5800
Lab Rat
39 posts

Volker wrote:
gnarf … of course it does not call the dragEnterEvent method of your item if the scene already accepts the drag!

If you eventually drop your drag – there is only one object to receive it – it’s that one, that accepted the dragEnter/dragMove events.

Hypothetically, if you allowed both to dragEnter, which one should get the drop at the end? Both? The top one (on z-order)? The bottom one? Rolling dices?

Yes I realized that ( after posting :D ) anyway modifing the above code like so to allow graphics scene to forward the event

  1. void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
  2. {
  3.     QGraphicsScene::dragMoveEvent(event);
  4.     SourceWidget *source = qobject_cast<SourceWidget *>(event->source());
  5.     if(source) {
  6.         event->setDropAction(Qt::CopyAction);
  7.         event->accept();
  8.     }
  9.  
  10. }
  11.  
  12. void Scene::dropEvent(QGraphicsSceneDragDropEvent *event)
  13. {
  14.     QGraphicsScene::dropEvent(event);
  15.     SourceWidget *source = qobject_cast<SourceWidget *>(event->source());
  16.     if(source) {
  17.         createWidget(static_cast<Widget::WidgetType>(event->mimeData()->text().toInt()), event->scenePos());
  18.         event->setDropAction(Qt::CopyAction);
  19.         event->accept();
  20.     }
  21. }

the drag enter and drop events are called for the items correctly.
as for who accepts the drop in my application that depend on the contents of mime type (scene accepts images for example and item accept text drops).

Thanks everyone for your help.

 Signature 

—-
My programs never have bugs, they just develop random features.

January 13, 2011

Volker Volker
Ant Farmer
5428 posts

So it does work now?

January 13, 2011

Olorin5800 Olorin5800
Lab Rat
39 posts

Yes it works now.

 Signature 

—-
My programs never have bugs, they just develop random features.

December 20, 2011

texas texas
Lab Rat
24 posts

I had the same problem. Olorin’s code worked for me. Thanks for the follow up.

 
  ‹‹ [Solved] QSplashScreen using a PNG with transparent areas      How to convert QImage to SDL_Surface ››

You must log in to post a reply. Not a member yet? Register here!