Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Parent QGraphicsItem with ItemIsMoveable, children with mouse event handling
QtWS25 Last Chance

Parent QGraphicsItem with ItemIsMoveable, children with mouse event handling

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marek
    wrote on last edited by
    #1

    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

    raven-worxR 1 Reply Last reply
    0
    • M Marek

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Marek
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Do accept in rectitem and do ignore in rectitem2

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          M 1 Reply Last reply
          5
          • dheerendraD dheerendra

            Do accept in rectitem and do ignore in rectitem2

            M Offline
            M Offline
            Marek
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

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

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              M 1 Reply Last reply
              5
              • dheerendraD dheerendra

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

                M Offline
                M Offline
                Marek
                wrote on last edited by
                #7

                @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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved