David Doria
David Doria

Reputation: 10273

itemChanged never called on QGraphicsItem

In this example: http://www.java2s.com/Code/Cpp/Qt/InteractwithQGraphicsItem.htm

the itemChange() function is where all of the updating work takes place. However, this function doesn't seem to be called when I move the handles around. According to:

http://doc.qt.io/qt-4.8/qgraphicsitem.html#itemChange

it should be called whenever the state changes, which I would imagine would include the position. Can anyone explain how to make this function get called when the handles are moved?

Upvotes: 2

Views: 4250

Answers (1)

pnezis
pnezis

Reputation: 12321

From the qt documentation check the QGraphicsItem::ItemPositionChange:

The item's position changes. This notification is sent if the ItemSendsGeometryChanges flag is enabled, and when the item's local position changes, relative to its parent (i.e., as a result of calling setPos() or moveBy()). The value argument is the new position (i.e., a QPointF). You can call pos() to get the original position. Do not call setPos() or moveBy() in itemChange() as this notification is delivered; instead, you can return the new, adjusted position from itemChange(). After this notification, QGraphicsItem immediately sends the ItemPositionHasChanged notification if the position changed.

So you should enable the flag QGraphicsItem::ItemSendsGeometryChanges in order to have the itemChange called when the position changes.

By default this is disabled for optimization:

For performance reasons, these notifications are disabled by default. You must enable this flag to receive notifications for position and transform changes. This flag was introduced in Qt 4.6.

In order to set a flag you should call the setFlag function preferably in the constructor of your custom item.

item->setFlag(GraphicsItem::ItemSendsGeometryChanges);

Upvotes: 6

Related Questions