Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt, use of "&" in tr

Tags:

qt

tr

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.

like image 809
Asy Avatar asked Dec 14 '22 18:12

Asy


2 Answers

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

like image 107
sepp2k Avatar answered Dec 22 '22 07:12

sepp2k


& 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.

like image 40
nobody Avatar answered Dec 22 '22 07:12

nobody