views:

376

answers:

1

Qt's QGraphicsScene provides some nice functionality out of box: user can select objects and move them around.

I want one more thing - get notifications when user has finished moving the object. There are lower level events, like mouse move, press, release, but I'd not like to reimplement the functionality that is already there (moving the objects).

The http://doc.trolltech.com/4.2/qgraphicsitem.html#itemChange method looks like it, but it is NOT called when user moves the object.

I'm using Qt 4.6

It turns out you have to set a flag to enable this event: item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

But now the event is fired every time item's corrdinates change. I want to have an event only when the change is done by the user.

+1  A: 

I think that the best way is implementing the mouseRelease event for your items, when it is fired you will be able to know if the item was moved or not. If the item was moved accept the event, else send the event to the base class.

For example:

 void YourItem::mouseReleaseEvent(QMouseEvent *event)
 {
     if (wasMoved()) {
         //do something and accept the event
     } else {
         // to the base class
         QGraphicsItem::mouseReleaseEvent(event);
     }
 }

WasMoved() is your method to know if the item was moved

Drewen
This is what i'm doing right now and it kinda works, but i'm updating the objects programmatically and the result it that on each mouse release ALL objects are considered moved even if there was no actual movement at all.
extropy
where do you write this function on your view/scene class or in your item class?
Drewen
This function in my item class.The nontrivial case is that there may be several selected objects that are moved, so i test all the items for being moved. I guess using QGraphicsScene::selectedItems() would give the correct result.
extropy