如何在QGraphicsItem派生类中接收鼠标事件? Qt的

时间:2021-06-28 23:17:17

There are already threads about this, but the solution I could find does not work. Solution: writing QGraphicsView::MousePressEvent(event); at the end of my MousePressEvent class in the QGraphicsView derived class. Both do not work. The QGraphicsItem-class does not receive the mouse events. This is my MousePressEvent in my QGraphicsView class:

已经有关于此的线索,但我找到的解决方案不起作用。解决方案:编写QGraphicsView :: MousePressEvent(event);在QGraphicsView派生类的MousePressEvent类的末尾。两者都不起作用。 QGraphicsItem类不接收鼠标事件。这是我的QGraphicsView类中的MousePressEvent:

void GraphWidget::mousePressEvent(QMouseEvent *event){
    mousePressed = true;

    if (event->button() == Qt::RightButton) // doesn't matter
    {
        rightMousePressed = true;
        _panStartX = event->x();
        _panStartY = event->y();
        setCursor(Qt::ClosedHandCursor);
        event->accept();
        return;
    }

    // And I tried this: QGraphicsView::mousePressEvent(event);
}

This is my MousePressEvent in my QGraphicsItem class:

这是我的QGraphicsItem类中的MousePressEvent:

void Node::mousePressEvent(QGraphicsSceneMouseEvent *event){
    mousePressed = true;
    qDebug() << "mouse trigered!";
}

Any ideas, what I've forgotten?

任何想法,我忘记了什么?

1 个解决方案

#1


0  

ANSWER Never forget to call

答案永远不要忘记打电话

QGraphicsView::mousePressEvent(event); /
QGraphicsView::mouseReleaseEvent(event); /
QGraphicsView::mouseMoveEvent(event);
...

at the end of every mouseEvent you overrode in your QGraphicsView derived class (in the mouseMoveEvent use QGraphicsView::mouseMoveEvent(event) and so on). Otherwise really strange things can happen. Also if you even did not use some of these in your QGraphicsItem derived class, call it in every event.

在每个mouseEvent的末尾,你在你的QGraphicsView派生类中进行覆盖(在mouseMoveEvent中使用QGraphicsView :: mouseMoveEvent(event)等等)。否则真的会发生奇怪的事情。此外,如果你甚至没有在QGraphicsItem派生类中使用其中的一些,请在每个事件中调用它。

#1


0  

ANSWER Never forget to call

答案永远不要忘记打电话

QGraphicsView::mousePressEvent(event); /
QGraphicsView::mouseReleaseEvent(event); /
QGraphicsView::mouseMoveEvent(event);
...

at the end of every mouseEvent you overrode in your QGraphicsView derived class (in the mouseMoveEvent use QGraphicsView::mouseMoveEvent(event) and so on). Otherwise really strange things can happen. Also if you even did not use some of these in your QGraphicsItem derived class, call it in every event.

在每个mouseEvent的末尾,你在你的QGraphicsView派生类中进行覆盖(在mouseMoveEvent中使用QGraphicsView :: mouseMoveEvent(event)等等)。否则真的会发生奇怪的事情。此外,如果你甚至没有在QGraphicsItem派生类中使用其中的一些,请在每个事件中调用它。