View Full Version : Registering QGraphicsItem Movement (Drag)

27th May 2010, 13:02

I'm currently drawing some graphics items (actually QGraphicsPixmapItem) to a scene being shown by a view. I set each item to be movable, as follows

this->setFlag( QGraphicsItem::ItemIsMovable, true );

And it works fine. I'd like to be able to know when they are actually being moved, like call a function. It seems they they do not have signals/slots, nor can I get events to work. What is the best way to accomplish this?


27th May 2010, 13:08
Sorry, I forgot to mention that I'm subclassing from QGraphicsPixmapItem.

27th May 2010, 13:11
If you're already subclassing, the I guess you can look into http://doc.qt.nokia.com/4.6/qgraphicsitem.html#itemChange

27th May 2010, 13:32
Okay so I've added the following:

QVariant ShipPart::itemChange ( GraphicsItemChange change, const QVariant & value )
if (change == ItemPositionChange && scene())
QPointF newPos = value.toPointF();
//Stuff will happen here
return QGraphicsItem::itemChange(change, value);

In debugging, I've found that the itemChange function gets called when I first add the item to the scene, as well as when I first make it visible. But when I move the item or just change it's position, it doesn't get called at all. Is that normal?

27th May 2010, 13:36
Yes, that's normal ;-)

Set that flag.

Edit: It's disabled for performance reasons

27th May 2010, 13:41
Perfect :) Thanks!