Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to prevent a QGraphicsItem from getting mousePressEvent from higher QGraphicsItem?
Qt 6.12 LTS is out! See what's new in the release blog

How to prevent a QGraphicsItem from getting mousePressEvent from higher QGraphicsItem?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 2 Posters 5.2k Views 1 Watching
  • 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.
  • NiagarerN Niagarer

    @mrjj
    Yes, like so:

    void myQGraphicsItemClass::mousePressEvent(QMouseEvent *event){
        QGraphicsItem::mousePressEvent(event);
        qDebug() << "mouse pressed";
        event->accept();
    }
    
    
    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #6

    @Niagarer
    and in .h its
    protected:
    virtual void mousePressEvent(QMouseEvent *event) override;

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #7

      Ah sorry
      its
      void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event)

      the other syntax is for QWidgets

      So its other type of event class. else concept should be the same
      so just change QMouseEvent to QGraphicsSceneMouseEvent and we
      are in business i hope.

      http://doc.qt.io/qt-5/qgraphicsitem.html#mousePressEvent

      NiagarerN 2 Replies Last reply
      2
      • mrjjM mrjj

        Ah sorry
        its
        void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event)

        the other syntax is for QWidgets

        So its other type of event class. else concept should be the same
        so just change QMouseEvent to QGraphicsSceneMouseEvent and we
        are in business i hope.

        http://doc.qt.io/qt-5/qgraphicsitem.html#mousePressEvent

        NiagarerN Offline
        NiagarerN Offline
        Niagarer
        wrote on last edited by
        #8

        @mrjj
        Ah, ok, I see, let me try...

        1 Reply Last reply
        0
        • mrjjM mrjj

          Ah sorry
          its
          void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event)

          the other syntax is for QWidgets

          So its other type of event class. else concept should be the same
          so just change QMouseEvent to QGraphicsSceneMouseEvent and we
          are in business i hope.

          http://doc.qt.io/qt-5/qgraphicsitem.html#mousePressEvent

          NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on last edited by Niagarer
          #9

          @mrjj
          It doesn't work... the mousePressEvent gets not executed... hm...
          Or I am stupid, I don't know...

          mrjjM 1 Reply Last reply
          0
          • NiagarerN Niagarer

            @mrjj
            It doesn't work... the mousePressEvent gets not executed... hm...
            Or I am stupid, I don't know...

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @Niagarer
            you are not stupid. :)

            make sure you have
            virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
            in h.

            notice the override.
            it should say if its not really overriding it

            NiagarerN 1 Reply Last reply
            0
            • mrjjM mrjj

              @Niagarer
              you are not stupid. :)

              make sure you have
              virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
              in h.

              notice the override.
              it should say if its not really overriding it

              NiagarerN Offline
              NiagarerN Offline
              Niagarer
              wrote on last edited by Niagarer
              #11

              @mrjj
              Yes, that is exactely what I wrote...
              Oh wait...

              mrjjM 1 Reply Last reply
              0
              • NiagarerN Niagarer

                @mrjj
                Yes, that is exactely what I wrote...
                Oh wait...

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @Niagarer
                hmm hmm
                And no erros or warning ?

                NiagarerN 1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  They use it here
                  http://doc.qt.io/qt-5/qtwidgets-graphicsview-elasticnodes-example.html

                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @Niagarer
                    hmm hmm
                    And no erros or warning ?

                    NiagarerN Offline
                    NiagarerN Offline
                    Niagarer
                    wrote on last edited by Niagarer
                    #14

                    @mrjj
                    Wooow, I just wrote mouseMoveEvent 2 minutes ago, copy and paste mistake.
                    Yes it works now, so the actual thing was the usage of QGraphicsSceneMouseEvent. And yes, the mousePressEvent of the item behind it, does not get triggered, wonderful.
                    Wonderful, thank you very much!

                    To sum it up:

                    #include <QGraphicsSceneMouseEvent>
                    void myQGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event){
                        QGraphicsItem::mousePressEvent(event); // call base
                        // do stuff
                        event->accept(); say its handled, so the items behind dont get the mouseClick
                    }
                    

                    @mrjj said in How to prevent a QGraphicsItem from getting mousePressEvent from higher QGraphicsItem?:

                    This goes for all Event type functions
                    if you say
                    event->accept();
                    you say "i handled it" and Qt will not try to give it to others

                    1 Reply Last reply
                    1
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #15

                      Oh sorry, it was my bad, i pasted the wrong one before. I guess u copied it and
                      like me didnt see it was move and not press :)
                      Good it works !

                      This goes for all Event type functions
                      if you say
                      event->accept();
                      you say "i handled it" and Qt will not try to give it to others

                      1 Reply Last reply
                      1

                      • Login

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