繁体   English   中英

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

[英]How to receive mouse events in QGraphicsItem derived class? Qt

已经有关于此的线程,但是我能找到的解决方案不起作用。 解决方案:编写QGraphicsView::MousePressEvent(event); 在QGraphicsView派生类的MousePressEvent类的末尾。 两者都不起作用。 QGraphicsItem-class不接收鼠标事件。 这是我的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);
}

这是我的QGraphicsItem类中的MousePressEvent:

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

有什么想法,我忘记了什么?

答案永远不要忘记打电话

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

在您在QGraphicsView派生类中覆盖的每个mouseEvent的末尾(在mouseMoveEvent中使用QGraphicsView :: mouseMoveEvent(event)等等)。 否则,可能会发生真正奇怪的事情。 同样,如果您甚至没有在QGraphicsItem派生类中使用其中的一些,则在每个事件中都调用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM