Important: Please read the Qt Code of Conduct -

Get Mouse Event to Function: Resolved

  • Linux Centos, C++
    I need help with a basic concept: How is a mouse event routed/directed to a function? The h file contains

    #include <qevent.h>
    void mouse_click_event( QMouseEvent * event );

    The dot cpp file contains

    Void C_Strip_Chart::mouse_click_event( QMouseEvent * event )
    { printf( “mouse event” ); }

    The printf is never called. The part I have not recognized is: What routes the mouse event to the function?
    Thank you for your time.

  • What is C_Strip_Chart derived from? Classes need to be derived from the QObject base to register events. Also, events are a lower level than signal/slot mechanism.

    You normally connect a widget's clicked() signal to a target slot that gets executed when the mouse is clicked on the widget....OH, and the base class QWidget can handle the mouse events through its protected virtuals.

    Events involve overriding protected virtuals that exist in the class, thus the reason for deriving from QObject.

  • Moderators

    @BKBK what's supposed to call mouse_click_event ? because even if QWidget is the base class, the is no orverrideable function with that name

  • C_Strip_Chart is the main and top level widget. It inherits from QWidget.
    What's supposed to call mouse_click_event? Both replies prompted me to think along the correct path. Changed mouse_click_event to mousePressEvent(...) and it started working.
    Not only that, I understand what I was doing wrong and why.

    Thank you both for your time and patience.

  • If C_Strip_Chart is derived from QWidget then simply overriding any of the protected virtuals should cause them to be executed. Use the docs, Luke. the QWidget page adequately shows all the relevant methods.

Log in to reply