Qt World Summit: Register Today!

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) {
    void RectItem2::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
        if(qAbs(point.x()-event->screenPos().x())>20 || qAbs(point.y()-event->screenPos().y())>20) {
    void RectItem2::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
        if(pressed) {
            emit clicked(item_id);

    Best Regards

  • Moderators

    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.

  • Qt Champions 2017

    Do accept in rectitem and do ignore in rectitem2

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

  • Qt Champions 2017

    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.

Log in to reply