Jonathan Jonathan - 3 months ago 8
Python Question

Using an '&' in a string in python

What is the purpose of the '&' in the string in this line?

exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)


Likewise here:

fileMenu = menubar.addMenu('&File')

Answer

The & character has a special purpose if you use it in the text of a QAction object: If the QAction object is used in a tool button, the character directly after the & symbol can be used as a keyboard shortcut for that button (in combination with the alt-key).

See http://doc.qt.io/qt-5/qaction.html (resp. http://doc.qt.io/qt-5/qaction.html#QAction-2).