1

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.

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
Nuno
  • 3,082
  • 5
  • 38
  • 58

2 Answers2

4

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

LeBlue
  • 595
  • 7
  • 11
2

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.

Azeem
  • 11,148
  • 4
  • 27
  • 40
Metoo
  • 400
  • 1
  • 7
  • Thank you for your suggestion. So, you wouldn't use Qt Creator to create the menus (as it changes the XML instead of creating C++ code); you would create the menus yourself with QMenu and QAction, so that you are able to set up the shortcuts. Is that correct? – Nuno Jan 22 '14 at 11:57
  • You can create menus in QtDesigner (or inside Qt Creator) if your window is a QMainWindow. You create the QAction instances and then drag-and-drop them into the menu. – RobbieE Jan 22 '14 at 15:44