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. Detecting clicks outside of widgets while still having widgets deal with keypresses that occur within them.

Detecting clicks outside of widgets while still having widgets deal with keypresses that occur within them.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.4k 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.
  • T Offline
    T Offline
    TrolliOlli
    wrote on last edited by TrolliOlli
    #1

    I'm trying to make a basic application that lets you click in two locations to define a line (e.g. I click at point (0,0) and point (10, 10) and a line is drawn between those two points).

    Additionally, I'd like to have a rectangle widget that I can click and drag around.

    I already have the rectangle working correctly, but now I'm running into issues detecting keyPresses in the main view.

    I have a custom class that inherits from QGraphicsView and overrides the following methods:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

    When I implement these methods however, my program completely ignores the mousePressEvent methods I have within my rectangle class (as they are all caught by these methods first).

    Is there a way to catch clicks within my view ONLY when they're not inside a widget (otherwise catch them within the widget)?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Are you calling the base class implementation of these methods in your reimplementation ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Are you calling the base class implementation of these methods in your reimplementation ?

        T Offline
        T Offline
        TrolliOlli
        wrote on last edited by
        #3

        @SGaist

        I don't believe so.

        In MyView I have:

        void MyView::mousePressEvent(QMouseEvent *event)
        {
        std::cout << "CLICKED IN VIEW\n" << std::endl;
        }

        and in MyRectangle I have:
        void MyRectangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
        {
        std::cout << "CLICKED IN Rectangle!\n" << std::endl;
        }

        Currently all clicks are printing "CLICKED IN VIEW", even if it's actually inside the rectangle.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TrolliOlli
          wrote on last edited by
          #4

          Bump. I still can't figure this out.

          The best way I can figure to do it currently is keep a list of MyRectangle's inside MyView and then manually pass the event like so:

          void MyView::mousePressEvent(QMouseEvent *event)
          {
              std::cout << "CLICKED IN VIEW\n" << std::endl;
          
              for (std::vector<MyRectangle*>::iterator it = rectangles.begin(); it != rectangles.end(); ++it)
              {
           	
          	    ((MyRectangle*)*it)->mousePressEvent(event);
          	
              }
          }
          

          this seems really barbaric though and I'm hoping there's just some way to do something like passEventToChildren(event)

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Again: you are not calling the base class implementation, so you are interrupting the normal event delivery.

            void MyView::mousePressEvent(QMouseEvent *event){
                qDebug() << "Clicked in view";
                QGraphicsView::mousePressEvent(event);
            }
            

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply
            0
            • SGaistS SGaist

              Again: you are not calling the base class implementation, so you are interrupting the normal event delivery.

              void MyView::mousePressEvent(QMouseEvent *event){
                  qDebug() << "Clicked in view";
                  QGraphicsView::mousePressEvent(event);
              }
              
              T Offline
              T Offline
              TrolliOlli
              wrote on last edited by
              #6

              @SGaist said:

              Again: you are not calling the base class implementation, so you are interrupting the normal event delivery.

              void MyView::mousePressEvent(QMouseEvent *event){
                  qDebug() << "Clicked in view";
                  QGraphicsView::mousePressEvent(event);
              }
              

              This worked perfectly. Sorry, I thought you were making sure I WASN'T calling the base class implementation.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                No problem :)

                Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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