SoFunction
Updated on 2025-05-14

Detailed explanation of the use of QMainWindow components in PyQt6

If QWidget is compared to a rough house, QMainWindow divides five rooms based on the rough house: menu bar, toolbar, central component, docking window, status bar. Each room has some decorative tools, which can better and faster decorate the room.

1. Overview of QMainWindow Components

QMainWindowProvides a standard application window structure, containing multiple important components:

  • Central Widget: This is the core area of ​​QMainWindow, which usually displays most of the application. Can be passedsetCentralWidget(widget)Method to set the central component.
  • Menu Bar:passmenuBar()Methods create the application's menu, allowing the addition of menu items.
  • Tool Bar:passaddToolBar()Methods Add toolbar to window.
  • Status Bar:passstatusBar()Method displays status information at the bottom of the window.
  • Dock Widgets:passaddDockWidget()Add dockable windows to the main window to increase the flexibility of the application interface.

2. Use QMainWindow

import sys

from  import Qt
from  import QAction
from  import *


class AppMainWindow(QMainWindow):
    def __init__(self):
        super(AppMainWindow, self).__init__()

        ("QMainWindow Case")
        (800, 600)

        # Central area        self.init_central()
        # Menu Bar        self.init_menubar()
        # Toolbar        self.init_toolbar()
        # Status Bar        self.init_statusbar()
        # Docking window        self.init_dock()

        # Signal-Slot        self.init_signal()

    def init_central(self):
        self.central_label = QLabel("I'm the central component")
        (self.central_label)

    def init_menubar(self):
        menubar = ()
        # Create menu        file_menu = ("document")
        # Add menu item        exit_action = QAction("quit", self)
        exit_action.()
        file_menu.addAction(exit_action)


    def init_toolbar(self):
        tool_bar = QToolBar("Toolbar")

        # Add tools to the toolbar        exit_action = QAction("quit",self)
        exit_action.()
        tool_bar.addAction(exit_action)

        (tool_bar)

    def init_statusbar(self):
        status_bar = QStatusBar()
        status_bar.showMessage("This is a status bar")
        (status_bar)

    def init_dock(self):
        dock_widget = QDockWidget("Stay window", self)
        self.list_widget = QListWidget()
        for i in range(5):
            item = QListWidgetItem(f'item {i}')
            self.list_widget.addItem(item)
        dock_widget.setWidget(self.list_widget)
        (, dock_widget)

    def init_signal(self):
        # Double-click the listwidget content        self.list_widget.(self.show_central_msg)

    def show_central_msg(self):
        current_item = self.list_widget.currentItem()

        if current_item.text():
            self.central_label.setText(current_item.text())



if __name__ == '__main__':
    app = QApplication()
    window = AppMainWindow()
    ()
    (())


3. Advanced functions of QMainWindow

3.1. Customization of window menus and toolbars

You can set shortcut keys for menu items and toolbar buttons, and dynamically update status bar information.

3.2. Multi-Document Interface (MDI) Support

QMainWindowSupportedQMdiAreaandQMdiSubWindowTo create a multi-document interface (MDI).

3.3. Event handling

QMainWindowSupport handling of common events such ascloseEvent()resizeEvent(), so that specific actions are performed when an event occurs.

4. Summary

QMainWindowIt is the basis for building desktop applications. It allows developers to focus on the functional implementation of applications by providing multiple standard widgets. Whether it is a menu bar, toolbar, status bar or docking window,QMainWindowIt can easily build a professional-level application interface.

This is the end of this article about the detailed explanation of the use of QMainWindow components in PyQt6. For more related content of PyQt6 QMainWindow components, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!