Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 :

    #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



  • @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.


  • Lifetime Qt Champion

    @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 :)



  • 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



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



  • 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



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



  • @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


Log in to reply