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):

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

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