[SOLVED] Catch Cmd+Q



  • Hi

    I'd like to catch Cmd+Q to prevent the application termination in some cases.
    I found this for win, but I'm not able to implement it under mac. Also didn't found any modifier for the command button like QT::ControlModifier.
    http://developer.qt.nokia.com/faq/answer/how_can_i_catch_altf4_in_my_qt_application

    Tried to accept the event, without checking the modifier, but no luck with that also

    Can somebody help me out?



  • i think you can use Qt::MetaModifier as the windows or cmd key.

    other than that the above should work, you could also reimplement

    @virtual bool QObject::event( QEvent * e );@

    and catch it there, but either method should work, it depends where you are trying to catch the key from.



  • @birmacher: The correct way to do this is to overload QWidget::closeEvent(). Take a look at the documentation for more "details":http://doc.troll.no/4.7/qwidget.html#closeEvent . Actually the FAQ is referring to another use-case than yours. It only refers to a hack which is meant to allow defining other than application termination logic on Alt+F4.



  • Thanks for the tips. I did override the closeEvent, with no success.
    Finally it turned out that I was catching the Qt::Close event also. Removing it did the trick:)

    Thanks for your help!



  • 6 years later, this is basically still the correct answer; when using class QGuiApplication without widgets, this is the way to go:

    bool MyDerivedFromQGuiApplication::event( QEvent* event_in )
    {
        if ( event_in->type() == QEvent::Close )
        {
            event_in->ignore();
            // INSERT: custom handling
            return true;
        }
        return QGuiApplication::event( event_in );
    }
    

Log in to reply
 

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