fatma.ekici
fatma.ekici

Reputation: 2837

Handling mousePressEvent for overlapping graphics items

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
}

Upvotes: 2

Views: 1174

Answers (2)

mhheydarchi
mhheydarchi

Reputation: 99

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

and do not call base class event like :

CriticalRegion::hoverEnterEvent(event)

Upvotes: 0

TheDarkKnight
TheDarkKnight

Reputation: 27629

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.

Upvotes: 1

Related Questions