PDA

View Full Version : Help programming dragging objects inside a QGraphicsScene



drake1983
8th July 2007, 22:50
Hi everybody! I need a little example about moving a picture or any object inside a QGraphicsScene Widget. The object or picture must be a GraphicsItem.

I tried with the examples of Qt4 Demo but I am looking for something simple.
I am developing an application where I use QGraphicsView, QGraphicsScene and QGraphicsItem, then I need to drag and drop the QGraphicsItem around the scene, but I don't find the way to do that.

Regards!

jpn
9th July 2007, 06:30
You can make a QGraphicsItem movable like this:

item->setFlag(QGraphicsItem::ItemIsMovable, true);
Also, you can restrict how and where an item can be moved by reimplementing QGraphicsItem::itemChange() (notice the example in docs).

drake1983
10th July 2007, 16:24
Thanks so much! It works!!

Now, I have a little problem, I have two GraphicsItems in the scene, when I move one of them, the other item also moves to the opposite direction.

I attached my program above.

jpn
10th July 2007, 16:51
I have two GraphicsItems in the scene, when I move one of them, the other item also moves to the opposite direction.
This is because no area is set for the scene, see QGraphicsScene::sceneRect() (http://doc.trolltech.com/4.3/qgraphicsscene.html#sceneRect-prop). If no scene rect is set, the scene grows when items are added and/or moved. The view can also be made to align scene (http://doc.trolltech.com/4.3/qgraphicsview.html#alignment-prop) differently and a resize anchor (http://doc.trolltech.com/4.3/qgraphicsview.html#resizeAnchor-prop) may be set.

drake1983
11th July 2007, 04:21
Thanks! Now everything works fine!!!