Qt 同时触发多个QGraphicsItem的mouseMoveEvent

Qt 同时触发多个QGraphicsItem的mouseMoveEvent,qt,mouseevent,qgraphicsitem,Qt,Mouseevent,Qgraphicsitem,当我选择几个QGraphicsItem(使用Ctrl键)时,我可以将它们一起移动,但mouseMoveEvent仅针对实际接收事件的项目触发。是否有办法使每个选定的项目都接收事件?我在Qt的医生里找不到 我可以将所选项目分组并在QGraphicsView的mouseMoveEvent中进行处理吗 非常感谢您的帮助:)我在阅读您的问题,但听起来在QGraphicsItem类上实现QGraphicsItem::itemChange可能会更好。每当位置发生变化时(无论是通过鼠标、键盘、编程等)都会调用

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

我可以将所选项目分组并在QGraphicsView的mouseMoveEvent中进行处理吗


非常感谢您的帮助:)

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


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

  • 子类
    qgraphicscene
    并实现
    mouseMoveEvent
  • 在鼠标移动事件中,使用
    itemAt
    功能检查事件位置是否有项目
  • 如果存在一个项目并选中了它(
    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);  
}
void mouseMoveEvent(QGraphicsSceneMouseEvent*mouseEvent)
{
QPointF mousePosition=mouseEvent->scenePos();
QGraphicsItem*pItem=itemAt(mousePosition.x(),mousePosition.y());
if(pItem==NULL)
{
qgraphicscene::mouseMoveEvent(mouseEvent);
返回;
}
if(pItem->isSelected()==false)
{
qgraphicscene::mouseMoveEvent(mouseEvent);
返回;
}
//获取所有选定项目
QList items=selectedItems();
for(无符号i=0;idoSomething();
qgraphicscene::mouseMoveEvent(mouseEvent);
}