Search code examples
c++qtexitkde-plasma

How can I make Ctrl+Q quit a QT5 app with no menubar


I'm working on a KDE app written in QT5 that has no menubar and no visible quit button. I'd like to make Ctrl+Q quit the app without having to bind the action to a user-visible GUI element. I'm having trouble getting using QAction for this:

    // Allow Ctrl+Q to quit the app
    QAction *actionQuit = KStandardAction::quit(QApplication::instance(), SLOT(quit()), this);
    actionQuit->setShortcut(QKeySequence(QKeySequence::Quit));

Doesn't work, obviously. What am I doing wrong, and/or if this approach is wrong, what's the sanest way to do it?


Solution

  • You need to add actionQuit to a widget, in order to make it listen to key events. Assuming that this is your widget:

    addAction(actionQuit);
    

    Furthermore, you may want to set the action's shortcut context to QApplicationShortcut, to make it application wide, e.g.

    actionQuit->setShortcutContext(QApplicationShortcut);