admin管理员组

文章数量:1278948

I'm using two items: 'ItemImage', which inherits QGraphicsItem, and 'ItemDrawing', which inherits QGraphicsPixmapItem. Both items are created in mainwindow constructor, added scene with scene->addItem().

MyWindow::MyWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MyWindowClass())
    , _scene(new QGraphicsScene(this))
    , _item_drawing(new ItemDrawing())
    , _item_image(new ItemImage())
{
    ...
    _scene->addItem(_item_drawing);
    _scene->addItem(_item_image);
    ui->view->setScene(_scene);
}

and both items call setAcceptHoverEvents(true); in their constructor, reimplement hoverEnterEvent and hoverMoveEvent, also, I reimplemented mouseMoveEvent of graphics view and called QGraphicsView::mouseMoveEvent.

/* ItemImage */
ItemImage::ItemImage(QGraphicsItem* parent)
    : QGraphicsItem(parent)
{
    setAcceptHoverEvents(true);
    setFlags(ItemIsSelectable | ItemIsMovable);
}

void ItemImage::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
{
    ...
    QGraphicsItem::hoverMoveEvent(event);
}


/* ItemDrawing */
ItemDrawing::ItemDrawing(QGraphicsItem* parent)
    : QGraphicsPixmapItem(parent)
    , _temp_measure()
{
    setZValue(1);
    setTransformationMode(Qt::SmoothTransformation);
    setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
    setAcceptHoverEvents(true);
}

void ItemDrawing::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
{
    ...
    QGraphicsItem::hoverMoveEvent(event);
}


/* GraphicsView which inherits QGraphicsView */
void GraphicsView::mouseMoveEvent(QMouseEvent* e)
{
    ...
    QGraphicsView::mouseMoveEvent(e);
}

I want to receive hover...Event on both items that are overlapping at the same coordinates. For now, only the highest z-order item receives the event(i.e. ItemDrawing that called setZValue(1)). If move the ItemImage out of the ItemDrawing, the ItemImage will also receive hover events. If create one item as a child of another, both will be received hover events.

My question is, is there no other way to receive hover events on both items that are overlapping at the same coordinates?

I couldn't find anything in any documentation about propagating events between two items that don't have a common parent. And I looked at Qt's implementation of QGraphicsScenePrivate::dispatchHoverEvent, but it doesn't seem to propagate the event to all items positioned under the mouse.

So my conclusion is that it seems impossible for both items to receive events, but I'm not sure if I'm right or if I'm missing something.

本文标签: cQGraphicsSceneHoverEvent is only propagated top most QGraphicsItemStack Overflow