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/