3

I've created an object Chartblock that implements QGraphicsItem. My goal is to create a grid of these objects, and when the mouse button is pressed (and held) and is drug over each block, perform something on each block as the cursor enters it.

Since a QGraphicsItem grabs the mouse events when it is clicked within it, other Items will not fire for the mouseMoveEvent. I then created an object based on the QGraphicsItemGroup to handle all the mouse events, but then I would need some way to pass mousePressEvent/mouseReleaseEvent as well as mouseMoveEvent to each child that the cursor is over.

Am I overthinking how to do this? It seems like such a simple action shouldn't be that difficult to create, but with QGraphicsItems holding onto the mouse events for itself, I'm not sure how to get around it. I've read similar situations, but nothing seems to give a straightforward answer.

Edit: I suppose a way to do this would keep track of the coordinates/sizes of every single QGraphicsItem I create in an array, then get the position of the cursor in the Group mouseMoveEvent, and see if there's a hit..

4

1 回答 1

1

我能够汇总一些类似的答案来创建解决方案;我放弃了将所有 QGraphicItem 放在一个组中的想法,并将它们直接放在场景中。随着场景抓取所有鼠标事件,我让 mouseMoveEvent 检查当前位置是否位于 QGraphicsItem 的顶部 - 如果是,则执行某些操作。

我仍然需要尝试让 itemAt() 为我自己的实现 QGraphicsItem 的类工作,因为 itemAt 只返回 QGraphicsItem,但我确信一些演员应该让它工作。

void ChartScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF mousePosition = event->scenePos();
    QGraphicsItem* pItem = this->itemAt(mousePosition.x(), mousePosition.y());
}
于 2011-12-18T23:58:00.723 回答