
- PyQt5 Tutorial
- PyQt5 - Home
- PyQt5 - Introduction
- PyQt5 - What’s New
- PyQt5 - Hello World
- PyQt5 - Major Classes
- PyQt5 - Using Qt Designer
- PyQt5 - Signals & Slots
- PyQt5 - Layout Management
- PyQt5 - Basic Widgets
- PyQt5 - QDialog Class
- PyQt5 - QMessageBox
- PyQt5 - Multiple Document Interface
- PyQt5 - Drag & Drop
- PyQt5 - Database Handling
- PyQt5 - Drawing API
- PyQt5 - BrushStyle Constants
- PyQt5 - QClipboard
- PyQt5 - QPixmap Class
- PyQt5 Useful Resources
- PyQt5 - Quick Guide
- PyQt5 - Useful Resources
- PyQt5 - Discussion
PyQt5 - QMenuBar, QMenu & QAction Widgets
A horizontal QMenuBar just below the title bar of a QMainWindow object is reserved for displaying QMenu objects.
QMenu class provides a widget which can be added to menu bar. It is also used to create context menu and popup menu. Each QMenu object may contain one or more QAction objects or cascaded QMenu objects.
To create a popup menu, PyQt API provides createPopupMenu() function. menuBar() function returns main window’s QMenuBar object. addMenu() function lets addition of menu to the bar. In turn, actions are added in the menu by addAction() method.
Following table lists some of the important methods used in designing a menu system.
Sr.No. | Methods & Description |
---|---|
1 |
menuBar() Returns main window’s QMenuBar object |
2 |
addMenu() Adds a new QMenu object to menu bar |
3 |
addAction() Adds an action button to QMenu widget consisting of text or icon |
4 |
setEnabled() Sets state of action button to enabled/disabled |
5 |
addSeperator() Adds a separator line in the menu |
6 |
Clear() Removes contents of menu/menu bar |
7 |
setShortcut() Associates keyboard shortcut to action button |
8 |
setText() Assigns text to action button |
9 |
setTitle() Sets the title of QMenu widget |
10 |
text() Retrieves the text associated with QAction object |
11 |
title() Retrieves the text associated with QMenu object |
QMenu object emits triggered() signal whenever any QAction button is clicked. Reference to the clicked QAction object is passed on to the connected slot function.
Example
In this example, first all reference to QMenuBar object of top level window (which has to be a QMainWindow object) is stored.
bar = self.menuBar()
File menu is added to the menu bar by addMenu() method.
file = bar.addMenu("File")
An action button in the menu may be a string or a QAction object.
file.addAction("New") save = QAction("Save",self) save.setShortcut("Ctrl+S") file.addAction(save)
A submenu is added to top level menu.
edit = file.addMenu("Edit") edit.addAction("copy") edit.addAction("paste")
triggered() signal emitted by file menu is connected to processtrigger() method, which receives QAction object causing the signal.
file.triggered[QAction].connect(self.processtrigger)
The complete code is as follows −
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class menudemo(QMainWindow): def __init__(self, parent = None): super(menudemo, self).__init__(parent) layout = QHBoxLayout() bar = self.menuBar() file = bar.addMenu("File") file.addAction("New") save = QAction("Save",self) save.setShortcut("Ctrl+S") file.addAction(save) edit = file.addMenu("Edit") edit.addAction("copy") edit.addAction("paste") quit = QAction("Quit",self) file.addAction(quit) file.triggered[QAction].connect(self.processtrigger) self.setLayout(layout) self.setWindowTitle("menu demo") def processtrigger(self,q): print q.text()+" is triggered" def main(): app = QApplication(sys.argv) ex = menudemo() ex.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
Output
The above code produces the following output −
