PDA

View Full Version : Registering QGraphicsItem Movement (Drag)



Polnareff
27th May 2010, 13:02
Hi,

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?

Thanks!

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

tbscope
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

Polnareff
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?

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

QGraphicsItem::ItemSendsGeometryChanges
Set that flag.

Edit: It's disabled for performance reasons

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