Catch Qt::BackButton mouse events no matter which Widget is under the mouse cursor



  • It seems that my
    @
    void MainWindow::mousePressEvent(QMouseEvent* event)
    @
    is only called when the mouse is not over controls that might also check for mousebuttons like an edit field. However, I want to globally catch the forward and backward mouse buttons to allow history navigation. Overriding mousePressEvent in QMainWindow does not seem to be the right way to do that then?


  • Moderators

    you can install an eventfilter on your qApp instance.
    @
    qApp->installEventFilter(...);
    @

    Or you could subclass QApplication and reimplement notify() and listen for the event with the Qt::BackButton. You could also define a signal then.
    @
    virtual bool notify( QObject * receiver, QEvent * event )
    {
    if( event->type() == QEvent::MouseButtonPress )
    {
    QMouseEvent* me = static_cast<QMouseEvent*>(event);
    switch( me->button() )
    {
    case Qt::BackButton:
    emit navigateBackTriggered();
    break;
    case Qt::ForwardButton:
    emit navigateForwardTriggered();
    break;
    }
    return QApplication::notify(receiver, event);
    }
    @

    I would prefer to use the notify() method since it's ensured you really get all events (also the ones delivered to disabled widgets).



  • Thanks, using the event filter works great!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.