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.
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
Except that it can't work always as you don't always get a mouseReleaseEvent...
The code that is doing the dragging is blocking code however, so you can put it over there, as the code will only continue after you released mouse button and it always works... (QT 4.7+)
精彩评论