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. mousePressEvent handler not called but enterEvent is
Forum Updated to NodeBB v4.3 + New Features

mousePressEvent handler not called but enterEvent is

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.1k 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.
  • D Offline
    D Offline
    Daniel Santos
    wrote on last edited by
    #1

    Hi,

    I have a QWidget derived class, that I am overriding events on.
    I can override enterEvent and leaveEvent.
    But mousePressEvent and mouseReleaseEvent aren't being called.
    On the enter and leave event handlers I even do a grabMouse and releaseMouse but nothing.
    Below is the code of the widget class :

    #ifndef XYPATHCONTROLLER_DRAWINGAREA_H
    #define XYPATHCONTROLLER_DRAWINGAREA_H
    
    #include <QGraphicsView>
    
    class DrawingArea : public QWidget {
        Q_OBJECT
    
    public:
        DrawingArea(QWidget* parent);
        ~DrawingArea();
    
    protected:
        void enterEvent(QEvent *event);
        void leaveEvent(QEvent *event);
        void mousePressEventA(QMouseEvent *event);
        void mouseReleaseEventA(QMouseEvent *event);
    };
    
    #endif //XYPATHCONTROLLER_DRAWINGAREA_H
    
    #include <iostream>
    #include <QWidget>
    #include <QMouseEvent>
    #include "DrawingArea.h"
    
    DrawingArea::DrawingArea(QWidget* parent) : QWidget(parent) {
    }
    
    DrawingArea::~DrawingArea() {
    }
    
    void DrawingArea::enterEvent(QEvent* event) {
        this->setCursor(Qt::CrossCursor);
        this->grabMouse();
    }
    
    void DrawingArea::leaveEvent(QEvent *event) {
        this->setCursor(Qt::ArrowCursor);
        this->releaseMouse();
    }
    
    void DrawingArea::mousePressEventA(QMouseEvent *event) {
        const QPointF start = event->localPos();
        std::cout << "start point : " << start.x() << " - " << start.y();
    }
    
    void DrawingArea::mouseReleaseEventA(QMouseEvent *event) {
        const QPointF end = event->localPos();
        std::cout << "start point : " << end.x() << " - " << end.y();
    }
    

    Any help is welcome.
    Regards

    jeremy_kJ 1 Reply Last reply
    0
    • D Daniel Santos

      Hi,

      I have a QWidget derived class, that I am overriding events on.
      I can override enterEvent and leaveEvent.
      But mousePressEvent and mouseReleaseEvent aren't being called.
      On the enter and leave event handlers I even do a grabMouse and releaseMouse but nothing.
      Below is the code of the widget class :

      #ifndef XYPATHCONTROLLER_DRAWINGAREA_H
      #define XYPATHCONTROLLER_DRAWINGAREA_H
      
      #include <QGraphicsView>
      
      class DrawingArea : public QWidget {
          Q_OBJECT
      
      public:
          DrawingArea(QWidget* parent);
          ~DrawingArea();
      
      protected:
          void enterEvent(QEvent *event);
          void leaveEvent(QEvent *event);
          void mousePressEventA(QMouseEvent *event);
          void mouseReleaseEventA(QMouseEvent *event);
      };
      
      #endif //XYPATHCONTROLLER_DRAWINGAREA_H
      
      #include <iostream>
      #include <QWidget>
      #include <QMouseEvent>
      #include "DrawingArea.h"
      
      DrawingArea::DrawingArea(QWidget* parent) : QWidget(parent) {
      }
      
      DrawingArea::~DrawingArea() {
      }
      
      void DrawingArea::enterEvent(QEvent* event) {
          this->setCursor(Qt::CrossCursor);
          this->grabMouse();
      }
      
      void DrawingArea::leaveEvent(QEvent *event) {
          this->setCursor(Qt::ArrowCursor);
          this->releaseMouse();
      }
      
      void DrawingArea::mousePressEventA(QMouseEvent *event) {
          const QPointF start = event->localPos();
          std::cout << "start point : " << start.x() << " - " << start.y();
      }
      
      void DrawingArea::mouseReleaseEventA(QMouseEvent *event) {
          const QPointF end = event->localPos();
          std::cout << "start point : " << end.x() << " - " << end.y();
      }
      

      Any help is welcome.
      Regards

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      @Daniel-Santos said in mousePressEvent handler not called but enterEvent is:

      Hi,

      I have a QWidget derived class, that I am overriding events on.
      I can override enterEvent and leaveEvent.
      But mousePressEvent and mouseReleaseEvent aren't being called.
      On the enter and leave event handlers I even do a grabMouse and releaseMouse but nothing.
      Below is the code of the widget class :

          void mousePressEventA(QMouseEvent *event);
          void mouseReleaseEventA(QMouseEvent *event);
      

      There's an extraneous trailing A in the event handler function names.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      6
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @jeremy_k said in mousePressEvent handler not called but enterEvent is:

        There's an extraneous trailing A in the event handler function names.

        With the proper c++11 keyword override the compiler would have told this already :)

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        D 1 Reply Last reply
        5
        • Christian EhrlicherC Christian Ehrlicher

          @jeremy_k said in mousePressEvent handler not called but enterEvent is:

          There's an extraneous trailing A in the event handler function names.

          With the proper c++11 keyword override the compiler would have told this already :)

          D Offline
          D Offline
          Daniel Santos
          wrote on last edited by
          #4

          Hi,

          It was late at night and I forgot to remove the extraneous 'A'. But here's the explanation for it :
          Because the methods aren't called (I set breakpoints in them and nothing happens when I press the mouse button),
          I named the methods with the extra 'A' and was trying to make manula connections. Then I realized that that is a bit
          silly, and this should work. So I was using the code like I pasted it in this post.
          Now I corrected the code (removed the 'A' and put override keyword after the method declarations in the .h)
          but I am getting the same results. The breakpoints are not triggered, and nothing is printed in the console.

          I'm stumped. This is very basic, but I don't know what I am missing here.

          Thanks and Reagrds

          1 Reply Last reply
          0
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            Which Qt version are you using. Add event filter to your code and check if mouse events are triggered.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Daniel Santos
              wrote on last edited by
              #6

              Hi,

              I am on the latest v5. I went to the documentation and learned that the QMouseEvent for QEvent::MouseButtonPress and QEvent::MouseButtonRelease can be catched by the general event handler bool event(QEvent) and then switch on the type.

              The methods I was trying to override are virtual in QWidget and are implemented in QGraphicsView.
              I think that's why they are not called in my case.

              Thanks,
              Regards

              JoeCFDJ 1 Reply Last reply
              0
              • D Daniel Santos

                Hi,

                I am on the latest v5. I went to the documentation and learned that the QMouseEvent for QEvent::MouseButtonPress and QEvent::MouseButtonRelease can be catched by the general event handler bool event(QEvent) and then switch on the type.

                The methods I was trying to override are virtual in QWidget and are implemented in QGraphicsView.
                I think that's why they are not called in my case.

                Thanks,
                Regards

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @Daniel-Santos it is technically same as event filter is used.

                1 Reply Last reply
                0
                • nageshN Offline
                  nageshN Offline
                  nagesh
                  wrote on last edited by
                  #8

                  @Daniel-Santos

                  The methods I was trying to override are virtual in QWidget and are implemented in QGraphicsView.
                  

                  After handling the events if you still wants it to propagate the events further you return "false"
                  https://doc.qt.io/qt-5/qobject.html#event

                  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