How to specify mnemonics (ampersand shortcut) on QActions in QMenu?

I use QActions in QMenu, typical

| &New file Ctrl+N | | &Open file Ctrl+O | 

What gets a good shortcut is simply N (for a new file) and O (for an open file) when the menu is open.

I would like to do something similar to list the latest files, i.e.:

 | [A recent file] Ctrl+1 | | [Another recent file] Ctrl+2 | ... etc 

It would be nice to set the mnemonics / context label for the corresponding 1 and 2 without having to include this number in the text box:

 | &1. [A recent file] Ctrl+1 | | &2. [Another recent file] Ctrl+2 | 

If anyone knows how to do this or can point me in the direction of clarification, I would be happy. I looked through some documents, and I cannot find much mention of using ampersand and equivalent ways to set a mnemonic shortcut for QActions.

Thanks.

Ps: Qt-4.7.4-rh6-x86_64, C ++

+4
source share
2 answers

You can create a shortcut valid only in the context of the menu:

 QAction * recentFileAction = new QAction( tr("A recent file"), this ); recentFileAction->setShortcut( QKeySequence( tr("Ctrl+1") ) ); QMenu * tools = menuBar()->addMenu( tr("&Tools") ); // Add a shortcut valid only when the tools menu has focus QShortcut * recentFileShortcut = new QShortcut( QKeySequence( tr("1") ), tools ); recentFileShortcut->setContext( Qt::WidgetShortcut ); connect( recentFileShortcut, SIGNAL(activated()), recentFileAction, SLOT(trigger())); 

You may need to set the menu focus policy to Qt::StrongFocus so that it accepts keyboard input.

+2
source

Post Notes:

  • Tested and confirmed work on linux / windows, using Qt-4.6.3-rh5-x86_64 and Qt-4.6.4-win32 respectively.
  • Tested and reported that does not work on Mac OS X Camille Klimek.

I'm not quite sure if this is the part of Qt designed to work, or just a hack. The fact that I can not find any documentation hints at the latter, but that the first one offers so beautifully. You will be the judge, and let me / us know.

Common use:

 // Existing: QMenu* fileMenu_ QAction* action = new QAction("Recent file name", fileMenu_) action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(1)))); fileMenu_->addAction(action); 

Now, apparently, Qt fills the file menu as a table with two columns. By default, a label (name) is used in the left column and formatted shortcut keys in the right column.

 | Recent file name Ctrl+1 | 

This can be easily configured using the shielded tab. Such that using:

 QAction* action = new QAction("Some text\tOther text", fileMenu_) action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(1)))); 

Results in

 | Some text Other text | 

Keeping the default keyboard shortcut Ctrl + 1 when you are out of focus. This leads to a solution:

 QAction* action = new QAction(QString("Recent file name\tCtrl+&%1").arg(i)), fileMenu_) action->setShortcut(QKeySequence(QString("CTRL+").append(QString::number(i)))); 

If the variable i denotes the index of the last file. This creates exactly what I had in mind, and also shows an underscore under the number, which perfectly reflects the mnemonic label.

Update

To demonstrate the end result, I added some images in case there is any confusion.

Providing Qt to fill in the desired column with a label (what I had before asking the question is pretty standard):

74VJz.png

After manually filling in the right column, as well as adding mnemonics:

KjttU.png

Which looks the same to me, except for the underline, denoting mnemonics.

0
source

Source: https://habr.com/ru/post/1394360/


All Articles