Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Why add '&' to the string?

Tags:

c++

qt

QPushButton *quitButton = new QPushButton("&Quit");

Why add a & to the Quit? Removing & and it seems the code behaves the same.

like image 979
CDT Avatar asked May 21 '13 09:05

CDT


2 Answers

The ampersand makes the button respond to a particular key-combination. In your case, if you press ALT + Q it will force the button to be pushed.

From Qt

The QPushButton widget provides a command button.

The push button, or command button, is perhaps the most commonly used widget in any graphical user interface. Push (click) a button to command the computer to perform some action, or to answer a question. Typical buttons are OK, Apply, Cancel, Close, Yes, No and Help.

A command button is rectangular and typically displays a text label describing its action. A shortcut key can be specified by preceding the preferred character with an ampersand in the text. For example:

 QPushButton *button = new QPushButton("&Download", this);

In this example the shortcut is Alt+D. See the QShortcut documentation for details (to display an actual ampersand, use '&&').

like image 159
bash.d Avatar answered Oct 13 '22 00:10

bash.d


The ampersand means that the letter 'Q' next to it will be used as a keyboard shortcut.

like image 40
Tadeusz A. Kadłubowski Avatar answered Oct 13 '22 01:10

Tadeusz A. Kadłubowski