2
votes

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:

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:

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

Any ideas, what I've forgotten?

1
Do you call "QGraphicsView::mousePressEvent(event);" in mousePressEvent function of your graphicsview class? Also check if the boundingRect() function is implemented properly in your graphicsitem class.fatma.ekici

1 Answers

0
votes

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.