I'm going through the Qt5 Application tutorial. In the creation of actions, I keep seeing the usage of "&" like the follwoing:
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this)
...
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
...
enter code exitAct = new QAction(tr("E&xit"), this);
...
I can't seem to find why these "&" are used, the Qt documentations on tr uses these notations without explaining what they are for. Further, when I deleted the "&" symbol, the resulting application doesn't seem to change at all.
As far as tr
is concerned &
is just another character in the string and has no special meaning.
However it does have a special meaning to QAction
: It sets the following character as the entry's shortcut within a menu, so when you have the "File" menu open, pressing the n key will activate the "New" entry, x will active "Exit" and so on. Depending on the OS, the character after the & may also be underlined (on Windows it's only underlined when you press the Alt key).
&
is used to indicate a character that should be used for the Alt-key shortcut for a given menu item. Assuming these actions are being put into a menu named &File
, pressing Alt-F, then N would activate the New
action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With