同时触发多个QGraphicsItem的mouseMoveEvent

发布于 2024-12-16 02:38:21 字数 201 浏览 6 评论 0原文

当我选择多个 QGraphicsItem(使用 Ctrl 键)时,我可以将它们移动到一起,但 mouseMoveEvent 仅针对实际接收事件的项目触发。有没有办法让每个选定的项目都收到该事件?我在 Qt 的文档中找不到它。

我可以将选定的项目分组在一起并在 QGraphicsView 的 mouseMoveEvent 中处理它吗?

非常感谢您的帮助:)

When I select several QGraphicsItem (with Ctrl key) I can move them together, but the mouseMoveEvent is triggered only for the item that actually receives the event. Is there a way to make every selected items receive the event ? I can't find it in Qt's doc.

Could I group selected items together and handle it within QGraphicsView's mouseMoveEvent ?

Thanks a lot for any help :)

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

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

发布评论

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

评论(2

听闻余生 2024-12-23 02:38:21

不,据我所知,没有默认的方法可以做你想做的事。您可以执行以下操作:

  • 子类 QGraphicsScene 并实现 mouseMoveEvent
  • 在鼠标移动事件中,使用 itemAt< 检查事件位置是否有项目/code> 函数
  • 如果有一个项目并且被选中(isSelected),则获取场景中所有选定的项目。
  • 对于所有选定的项目,请调用您要调用的相同函数。

示例代码如下:

void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) 
{
    QPointF mousePosition = mouseEvent->scenePos();
    QGraphicsItem* pItem = itemAt(mousePosition.x(), mousePosition.y());
    if (pItem == NULL)
    {
        QGraphicsScene::mouseMoveEvent(mouseEvent);
        return;
    }

    if (pItem->isSelected() == false)  
    {
        QGraphicsScene::mouseMoveEvent(mouseEvent);
        return;
    }

    // Get all selected items
    QList<QGraphicsItem *> items = selectedItems();

    for (unsinged i=0; i<items.count(); i++)
        // Do what you want to do when a mouse move over a selected item.
        items[i]->doSomething(); 

    QGraphicsScene::mouseMoveEvent(mouseEvent);  
}

No there is no default way to do what you want as far as I know. Something you could do is the following:

  • Subclass QGraphicsScene and implement the mouseMoveEvent
  • In the mouse move event check if there is an item at the event position using the itemAt function
  • If there is an item and it is selected (isSelected), get all selected items of the scene.
  • For all selected items call the same function you would call.

Sample code follows:

void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) 
{
    QPointF mousePosition = mouseEvent->scenePos();
    QGraphicsItem* pItem = itemAt(mousePosition.x(), mousePosition.y());
    if (pItem == NULL)
    {
        QGraphicsScene::mouseMoveEvent(mouseEvent);
        return;
    }

    if (pItem->isSelected() == false)  
    {
        QGraphicsScene::mouseMoveEvent(mouseEvent);
        return;
    }

    // Get all selected items
    QList<QGraphicsItem *> items = selectedItems();

    for (unsinged i=0; i<items.count(); i++)
        // Do what you want to do when a mouse move over a selected item.
        items[i]->doSomething(); 

    QGraphicsScene::mouseMoveEvent(mouseEvent);  
}
花间憩 2024-12-23 02:38:21

我正在阅读您问题的字里行间,但听起来在您的 QGraphicsItem 类上实现 QGraphicsItem::itemChange 可能会更好。每当位置发生变化时(无论是通过鼠标、键盘、编程等),都会调用此函数。如果您愿意,您甚至可以取消更改。

I'm reading between the lines of your question a little, but it sounds like you might be better served by implementing QGraphicsItem::itemChange on your QGraphicsItem class(es). This will get called whenever the position changes--whether by mouse, keyboard, programmatic, etc. You can even cancel the change if you want to.

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