如何获取QGraphicsScene报表项移动事件

发布于 2024-08-17 05:02:46 字数 503 浏览 12 评论 0原文

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

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

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

我正在使用 Qt 4.6

事实证明你必须设置一个标志才能启用此事件: item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

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

Qt's QGraphicsScene provides some nice functionality out of box: user can select objects and move them around.

I want one more thing - get notifications when user has finished moving the object.
There are lower level events, like mouse move, press, release, but I'd not like to reimplement the functionality that is already there (moving the objects).

The http://doc.trolltech.com/4.2/qgraphicsitem.html#itemChange method looks like it, but it is NOT called when user moves the object.

I'm using Qt 4.6

It turns out you have to set a flag to enable this event:
item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

But now the event is fired every time item's corrdinates change. I want to have an event only when the change is done by the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-08-24 05:02:46

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

例如:

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

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

I think that the best way is implementing the mouseRelease event for your items, when it is fired you will be able to know if the item was moved or not. If the item was moved accept the event, else send the event to the base class.

For example:

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

WasMoved() is your method to know if the item was moved

噩梦成真你也成魔 2024-08-24 05:02:46

只是它不能总是工作,因为您并不总是获得 mouseReleaseEvent...

但是,执行拖动的代码是阻塞代码,因此您可以将其放在那里,因为代码只有在释放鼠标后才会继续按钮,它总是有效...(QT 4.7+)

Except that it can't work always as you don't always get a mouseReleaseEvent...

The code that is doing the dragging is blocking code however, so you can put it over there, as the code will only continue after you released mouse button and it always works... (QT 4.7+)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文