
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
QMenuBar, QMenu & QAction Widgets
The QMenuBar is, like a menu strip at the top of an app window where you can find menus to access features and choices, in the application.
The QMenu is, like a menu that pops up when you do something on the screen by clicking on a button or something else, in the interface. It can have options, submenus and lines to separate them.
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 windows QMenuBar object. addMenu() function lets addition of menu to the bar. In turn, actions are added in the menu by addAction() method.
Methods used in designing a Menu System
Following table lists some of the important methods used in designing a menu system.
Given below are the most commonly used methods of QMenu.
Sr.No. | Methods & Description |
---|---|
1 |
menuBar() Returns main windows 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 1: QMenuBar
In this example, we create a simple PyQt application with a QMainWindow containing a QMenuBar. We add a "File" menu to the menu bar, along with an "Exit" action that closes the application when triggered.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow from PyQt6.QtGui import QAction class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): menubar = self.menuBar() file_menu = menubar.addMenu('File') exit_action = QAction('Exit', self) exit_action.triggered.connect(self.close) file_menu.addAction(exit_action) self.setWindowTitle('QMenuBar Example') self.setGeometry(300, 300, 400, 300) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
Output
The above code produces the following result −

Example 2: QMenu
In this example, we add an "Edit" menu to the menu bar, along with a submenu titled "Submenu." This submenu is created using QMenu and added to the "Edit" menu.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QMenu from PyQt6.QtGui import QAction class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): menubar = self.menuBar() file_menu = menubar.addMenu('File') edit_menu = menubar.addMenu('Edit') edit_submenu = QMenu('Submenu', self) edit_menu.addMenu(edit_submenu) exit_action = QAction('Exit', self) exit_action.triggered.connect(self.close) file_menu.addAction(exit_action) self.setWindowTitle('QMenu Example') self.setGeometry(300, 300, 400, 300) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
Output
The above code produces the following result −

Example 3: QAction
An QAction signifies a user triggered action commonly linked to menu options, toolbar icons or keyboard shortcuts.
In this example, we add "Copy" and "Paste" actions to the submenu. These actions can be triggered by the user and perform specific tasks when activated.
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QMenu from PyQt6.QtGui import QAction class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): menubar = self.menuBar() file_menu = menubar.addMenu('File') edit_menu = menubar.addMenu('Edit') edit_submenu = QMenu('Submenu', self) edit_menu.addMenu(edit_submenu) exit_action = QAction('Exit', self) exit_action.triggered.connect(self.close) file_menu.addAction(exit_action) copy_action = QAction('Copy', self) paste_action = QAction('Paste', self) edit_submenu.addAction(copy_action) edit_submenu.addAction(paste_action) self.setWindowTitle('QAction Example') self.setGeometry(300, 300, 400, 300) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())
Output
The above code produces the following result −
