如何在 QGraphicsView 中拖动鼠标时量化多个 QGraphicsItem 的位置?

发布于 2024-11-28 16:04:19 字数 876 浏览 0 评论 0原文

您好,感谢您的阅读。在 QGraphicsView 中拖动鼠标时,我无法正确量化多个 QGraphicsItems 的位置。如果一次只拖动一个,我设置的系统可以正确量化 QGraphicsItem,但是如果我选择了多个并拖动它们,则只有主要项目(直接位于鼠标下方的项目)被量化,其余项目的位置连续设置。我将非常感谢任何对此的帮助。相关代码如下:

这是一个名为 MutaEvent 的类,该类继承自 QGraphicsRectItem。我重新定义了 mouseMoveEvent() 和 setPos() 函数:

void MutaEvent::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
    QGraphicsRectItem::mouseMoveEvent(event);
    setPos(pos());
}

void MutaEvent::setPos(const QPointF &pos)
{
    QGraphicsRectItem::setPos(Muta::quantizePointD(pos,30,15));
    emit posChanged(objectID,pos);
}

下一位是名为 Muta 的命名空间中的静态函数:

static QPointF quantizePoint(QPointF point,double xQuant, double yQuant)
{
    double x = quantize(point.x(),xQuant);
    double y = quantize(point.y(),yQuant);
    QPointF quantPoint(x,y);
    return quantPoint;
}

任何帮助将不胜感激!

Hello and thanks for reading. I am having trouble correctly quantizing the position of multiple QGraphicsItems while dragging the mouse in a QGraphicsView. The system I have setup is correctly quantizing a QGraphicsItem if only drag one at a time, however if I have multiple selected and drag them, only the primary item(the one directly under the mouse) is quantized, the rest have their positions set continuously. I would very much appreciate any help with this. The relevant code follows:

This is in a class called MutaEvent which inherits from QGraphicsRectItem. I have redefined the mouseMoveEvent() and setPos() functions:

void MutaEvent::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
    QGraphicsRectItem::mouseMoveEvent(event);
    setPos(pos());
}

void MutaEvent::setPos(const QPointF &pos)
{
    QGraphicsRectItem::setPos(Muta::quantizePointD(pos,30,15));
    emit posChanged(objectID,pos);
}

the next bit is a static function in a namespace called Muta:

static QPointF quantizePoint(QPointF point,double xQuant, double yQuant)
{
    double x = quantize(point.x(),xQuant);
    double y = quantize(point.y(),yQuant);
    QPointF quantPoint(x,y);
    return quantPoint;
}

Any help would be much appreciated!

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

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

发布评论

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

评论(2

花开浅夏 2024-12-05 16:04:20

所有选定的项目都是您的 MutaEvent 类吗? setPos() 会在每个选定的项目上调用,因此如果没有调用,它将仅使用默认实现。

如果您希望它正常工作,您可能需要实现自己的鼠标事件处理(顺便说一句,在 MutaEvent::mouseMoveEvent 中调用 setPos(pos()) 将不会产生任何效果)。

Are all the selected items your MutaEvent class? setPos() is called on each selected item so if not it would just use the default implementation.

You might need to implement your own mouse event handling if you want it to work properly (and by the way, calling setPos(pos()) in MutaEvent::mouseMoveEvent will have no effect).

忱杏 2024-12-05 16:04:19

看一下重写 QGraphicsItem::itemChange 受保护函数。当项目位置即将更改 (QGraphicsItem::ItemPositionChange) 时,您可以从那里收到通知,并有机会修改该值。无论更改是如何启动的(鼠标移动、组的一部分、在代码中设置等),都会调用此方法。

我怀疑您的部分问题是 QGraphicsItem::setPos()不是虚拟的,这意味着如果调用者将您的 MutaEvent* 实例视为 ,您的 setPos() 函数将不会被调用QGraphicsItem*。 Qt 框架中的任何地方都是这种情况,因为它们当然不知道您的 MutaEvent 类。这就是他们提供虚拟 itemChange 方法的原因。

Take a look at overriding the QGraphicsItem::itemChange protected function. From there you can be notified when an item position is about to change (QGraphicsItem::ItemPositionChange) and have the opportunity to modify the value. This method is called no matter how the change was initiated (mouse move, part of group, set in code, etc.)

I suspect part of your problem is that QGraphicsItem::setPos() is not virtual, which means that your setPos() function will not be called if a caller is treating an instance of your MutaEvent* as a QGraphicsItem*. This would be the case everywhere in the Qt framework since, of course, they have no knowledge of your MutaEvent class. This is why they provide the virtual itemChange method.

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