Search code examples
c++qtkeyboard-shortcutsqt-creatorqkeysequence

Qt Creator - Add keyboard shortcuts to Menu entries


I created the shortcut events, such as:

new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));

But now I would like to show "Ctrl+Q" in the menu entry here:

Exit

How do I do that? I don't seem to find a way to do that in Qt Creator.


Solution

  • Generally you would use QAction class for this, so you would have a QMenu to which you will add actions, in your case CLOSE. Then you can use SetShortcut to add "CTRL + Q" in menu.

    pNewAction->setShortcut(QString(strAccel.c_str()));
    

    where pNewAction is of type QAction.