2
votes

I have two different classes inheriting QGraphicsItem. Both of them have their specific mousePressEvent functions. When these items overlap on the scene, I want to execute only one of the items' mousePressEvent. How can I achieve this?

void PpiTargetItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
    mTargetColor = Qt::red;
    event->setAccepted(true);
}

// Bigger Item
void CriticalRegion::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
    LOGGER_START
    mCorners[0] = new CornerGrabber(this, 0);
    mCorners[1] = new CornerGrabber(this, 1);
    mCorners[2] = new CornerGrabber(this, 2);
    mCorners[3] = new CornerGrabber(this, 3);

    mCorners[0]->installSceneEventFilter(this);
    mCorners[1]->installSceneEventFilter(this);
    mCorners[2]->installSceneEventFilter(this);
    mCorners[3]->installSceneEventFilter(this);

    setCornerPositions();
    event->setAccepted(true);
    LOGGER_END
}
2
have one accept the eventratchet freak
By the way, there is a sceneEventFilter() in one of the items (no mousePressEvent function) and I think it might be preventing the other item to take mousePress events.fatma.ekici

2 Answers

1
votes

When you inherit from QGraphicsItem and implement the function

void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event)

Call event->accept() when the object wanting to deal with the event has done so. If this doesn't work for you, please add example code to your question.

0
votes

1- Set zValue of Grpahics 2- accept the event by calling : event->accept()

and do not call base class event like :

CriticalRegion::hoverEnterEvent(event)