在这个例子中: http://www.java2s.com/Code/Cpp/Qt/InteractwithQGraphicsItem.htm
itemChange() 函数是所有更新工作发生的地方。但是,当我四处移动 handle 时,似乎没有调用此函数。根据:
http://doc.qt.io/qt-4.8/qgraphicsitem.html#itemChange
只要状态发生变化就应该调用它,我想这会包括位置。任何人都可以解释如何在 handle 移动时调用此函数吗?
最佳答案
来自qt documentation检查 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 callingsetPos()
ormoveBy()
). The value argument is the new position (i.e., aQPointF
). You can callpos()
to get the original position. Do not callsetPos()
ormoveBy()
initemChange()
as this notification is delivered; instead, you can return the new, adjusted position fromitemChange()
. After this notification,QGraphicsItem
immediately sends theItemPositionHasChanged
notification if the position changed.
因此,您应该启用标志 QGraphicsItem::ItemSendsGeometryChanges
,以便在位置更改时调用 itemChange
。
默认为 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.
为了设置标志,您应该调用 setFlag
最好在自定义项目的构造函数中运行。
item->setFlag(GraphicsItem::ItemSendsGeometryChanges);
关于qt - itemChanged 从未调用过 QGraphicsItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8187807/