0

I'm learning PyQt5 at the moment and now I'm studying the QAction method in order to perform and action from the menu of my application

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon

class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

def initUI(self):
    exitAct = QAction(QIcon('exit.png'), '&Exit', self)
    exitAct.setShortcut('Ctrl+Q')
    exitAct.setStatusTip("Exit application")
    exitAct.triggered.connect(qApp.exit)

    self.statusBar()

    menubar = self.menuBar()
    fileMenu = menubar.addMenu('&File')
    fileMenu.addAction(exitAct)
    self.setGeometry(300,300,300,200)
    self.setWindowTitle('Simple Menu')
    self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

What I'm wondering in QAction function and addMenu(), what is the '&' in '&Exit' and '&File' standing for ? Or at least are they necessary to be kept ? Even though I deleted them it didn't change anything to their appearance or to the action they were meant to perform

Because I see it very often in scripts written by other programmers am I missing something ?

Rickantonais
  • 467
  • 4
  • 12
  • It makes the following letter the [mnemonic/accelerator key](https://doc.qt.io/qt-5/qshortcut.html#details), which is usually indicated by an underline (depending on the platform). – ekhumoro Jan 31 '19 at 18:39
  • Possible duplicate of [Qt - Why add '&' to the string?](https://stackoverflow.com/questions/16666060/qt-why-add-to-the-string) – ekhumoro Jan 31 '19 at 18:41
  • Thank you very much ! yes it's the same problem in a different language – Rickantonais Jan 31 '19 at 20:44

0 Answers0