qt - 如何获取QGraphicsScene上报item移动事件

标签 qt qgraphicsview

Qt 的 QGraphicsScene 提供了一些很好的开箱即用功能:用户可以选择对象并四处移动它们。

我还想要一件事 - 当用户完成移动对象时获得通知。 有较低级别的事件,如鼠标移动、按下、释放,但我不想重新实现已经存在的功能(移动对象)。

http://doc.trolltech.com/4.2/qgraphicsitem.html#itemChange方法看起来很像,但是当用户移动对象时它不会被调用。

我正在使用 Qt 4.6

原来你必须设置一个标志来启用这个事件: item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

但现在每次项目的坐标更改时都会触发该事件。我只想在用户完成更改时才发生事件。

最佳答案

我认为最好的方法是为您的项目实现 mouseRelease 事件,当它被触发时您将能够知道该项目是否被移动。如果项目被移动接受事件,否则将事件发送到基类。

例如:

 void YourItem::mouseReleaseEvent(QMouseEvent *event)
 {
     if (wasMoved()) {
         //do something and accept the event
     } else {
         // to the base class
         QGraphicsItem::mouseReleaseEvent(event);
     }
 }

WasMoved() 是您了解项目是否已移动的方法

关于qt - 如何获取QGraphicsScene上报item移动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2006433/

相关文章:

c++ - 带有 Qt 的动态 QGridLayout(适应小部件大小)

c++ - 为什么设计器生成的嵌入为 "Aggregation"的 UI 类无法实现自定义插槽?

c++ - QGraphicsView/QGraphicsScene渲染问题

qt4 - 单个 QGraphicsScene 的多个 QGraphicsView

c++ - QGraphicsScene位置问题

python - 在 PyQt 中设置 QGraphicsTextItem 来源

c++ - 当我的数组达到一定大小时 Qt 不工作

c++ - QMessageBox 在关闭时删除

c++ - 如何使用分隔符 '-' 将 QByteArray 转换为 QString?

python - 放大鼠标位置 QGraphicsView