gpt4 book ai didi

qt - itemChanged 从未调用过 QGraphicsItem

转载 作者:行者123 更新时间:2023-12-01 15:32:44 29 4
gpt4 key购买 nike

在这个例子中: 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 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.

因此,您应该启用标志 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com