PDA

View Full Version : Drag and drop Items in QGraphicsScene



MinMin
27th March 2014, 05:53
Hi guys,
I have a number of image items (QGraphicsPixmapItem) which are arranged in rows and columns in QGraphicsScene. I want to implement something like when drag an image item, that image will be dropped at the end and the following images will move forwards (replace the moved image).
I found the similar example, but it implements in QListWidget. And I dont know how to implement it in QgraphicsScene.
http://qt-project.org/doc/qt-5/qtwidgets-draganddrop-puzzle-example.html
Does anybody have any idea how to do that?
Thanks in advance :)

wysota
27th March 2014, 09:01
Reimplement mouse events for the scene and handle all the logic there.

MinMin
27th March 2014, 10:06
Yeah, I reimplement these events:
- mousePressEvent for the QGraphicsPixmapItem: to store the selected item's data (location and pixmap) and create QDrag
- dragEnterEvent, dragMoveEvent, and dropEvent for the QGraphicsScene: in dropEvent, retrieve the item's data and add the item into the scene.
The problem is how to update location of image items in the scene: selected item goes to end, following items go forwards. Any suggestions?

wysota
27th March 2014, 10:45
Don't reimplement drag events. Reimplement mouse events only -- mousePress, mouseMove and mouseRelease events. You don't need anything else. To be honest you can do even without reimplementing those events but instead handling itemChanged() but if you don't have experience with that, it might be easier with mouse events.

MinMin
27th March 2014, 13:31
Thank you for your advice :) I will try and see how :D