Parent QGraphicsItem with ItemIsMoveable, children with mouse event handling



  • Hi

    I have parent "RectItem" derived from QGraphicsRectItem with ItemIsMovable flag set. It has childern also derived from QGraphicsRectItem, children should handle mouse press/move/release event so they can emit signal when they are clicked.
    The problem is that parent doesn't move when child is pressed. It looks like the event is "consumed" by children and is not propagated to parent.
    How should I solve this?

    This is simple code for children:

    void RectItem2::mousePressEvent(QGraphicsSceneMouseEvent *event) {
        QGraphicsRectItem::mousePressEvent(event);
        event->accept();
        point=event->screenPos();
        pressed=true;
    }
    void RectItem2::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
        QGraphicsRectItem::mouseMoveEvent(event);
        if(qAbs(point.x()-event->screenPos().x())>20 || qAbs(point.y()-event->screenPos().y())>20) {
            pressed=false;
        }
    }
    void RectItem2::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
        QGraphicsRectItem::mouseReleaseEvent(event);
        if(pressed) {
            emit clicked(item_id);
        }
    }
    

    Best Regards
    Marek


  • Moderators

    @Marek
    event->accept(); prevents the event from being propagated up the parent hierachy



  • Hi

    But if I don't accept event in press, then children doesn't receive move and release and doesn't emit signal I want.



  • Do accept in rectitem and do ignore in rectitem2



  • @dheerendra So if I ignore in rectitem2 will it receive mouse move and release event ?



  • re-implement mousePressEvent() do processing and then ignore using ignore() API. It will be passed to parent



  • @dheerendra Can't do that, I need to emit signal in mouseReleaseEvent in child item, not in mousePressEvent.
    I have solved this that way:
    Parent rectItem has no ItemIsMovable flag, and child rectItem2 handles mouse press/move/release event. I have also subclassed QGraphicsScene and reimplemented mouse press/move/release event from scene. Now I'm sending custom signal from scene to some class that performs parent rectItem movement. That way parent rectItem is moving and child rectItem2 handles mouse events.
    It's only way to do it, I don't know any other.

    Thanks everyone for hints.
    Marek


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.