Detailed usage of menu and action for pyqt5

  • 2021-06-29 11:29:25
  • OfStack

As follows:


 exitAct = QAction(QIcon('exit.png'), '&Exit', self)    
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')

QAction is an abstraction for actionsperformed with a menubar, toolbar, or with a custom keyboard shortcut.

QAction module: menu bar or toolbar, or action performed by the software after customizing a hotkey.

Line 1 specifies an instance with a name and corresponding icon.Set Shortcut on Line 2


exitAct.triggered.connect(qApp.quit)
When we select this particular action, a triggered signal is emitted. The signal is connected to the quit() method of theQApplication widget. This terminates the application.

When an instance of the above definition is selected, the triggered signal is generated, which is associated with qApp.quit and can also be associated with other methods.

Associate the above actions to a menu item


fileMenu.addAction(exitAction)

Related articles: