Qt Creator - Add keyboard shortcuts to Menu entries

8k views Asked by At

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.

2

There are 2 answers

2
Metoo On BEST ANSWER

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.

0
LeBlue On

You can set the shortcut keys in the QtDesigner in the 'Action Editor' (Tab at the bottom, the 'Signal/Slot Editor' tab is selected by default).

Here all defined QActions are listed. A double click on a field in the 'shortcut' column will open the wanted dialog.

This will add the shortcut to the QAction (create triggered events) and set it as visibile text, e.g. in the menu entry (only visible in the application, not in QtDesigner).

In the 'Property Editor' are more options for the 'shortcuts' (e.g. make them translatable).

Add shortcuts to QActions in the Action Editor