qt - itemChanged 从未调用过 QGraphicsItem

标签 qt qt4

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

相关文章:

c++ - 使用 C++ QT 创建一个 ListView

c++ - 错误: reference to ‘int64’ is ambiguous Compiling problem opencv + dlib in QT

android - Necessitas (Qt/Android NDK) qmake 路径

qt - 隐藏的QWidget对象接收信号

qt4 - Qt 应用程序无法正确调整大小

Qt:在手动激活应用程序之前不会出现主窗口

android - Qt Quick Controls 2 缩放

c++ - 将 QT 32 位库与 64 位应用程序(MinGW64 编译器)链接起来?

c++ - 如何使用Qt获取主机名?

c++ - 如何将 X11 事件传递给 QDialog