SoFunction
Updated on 2025-05-14

Summary of the difference between QWidget and QMainWindow in PyQt6

QWidgetandQMainWindowis two commonly used classes in PyQt, which have significant differences in function and purpose. The following is a detailed comparison and description:

1. QWidget

Introduction

  • QWidgetIt is the base class for all widgets (controls) in PyQt.
  • It is a general widget that can be used as a container (parent window) or as a standalone control.

Features

  • Lightweight: only provides basic window features, such as size, position, background, etc.
  • High flexibility: freely combined and customized.
  • No built-in layout structure: need to add layout manually (such asQVBoxLayoutorQHBoxLayout) and controls.
  • Suitable for use as small windows or nested containers: such as dialog boxes, custom controls, auxiliary windows, etc.

Common usage

  • Create custom controls.
  • As a dialog window (e.g.QDialogInherited fromQWidget)。
  • Nested in other widgets for use.

Simple example

from  import QApplication, QWidget, QLabel, QVBoxLayout

app = QApplication([])

# Create a QWidget windowwindow = QWidget()
("QWidget Example")

# Add controllayout = QVBoxLayout()
(QLabel("This is a QWidget"))
(layout)

()
()

2. QMainWindow

Introduction

  • QMainWindowis a class for creating main windows, providing advanced features and layout management.
  • It isQWidgetSubclass of  inheritedQWidgetfeatures and extends some features specifically for the main window.

Features

  • Built-in layout structure:
    • Includes menu bar (QMenuBar), toolbar (QToolBar), status bar (QStatusBar), docking window (QDockWidget) and central widgets.
  • Suitable for complex main window applications: suitable as the main interface for applications, supporting multiple sub-window management and advanced layout requirements.
  • Supports central widgets:
    • Need to passsetCentralWidget()Method sets the main content area.

Common usage

  • Create the main window of the full desktop application.
  • Used for versatile and complex user interfaces.

Simple example

from  import QApplication, QMainWindow, QLabel, QStatusBar

app = QApplication([])

# Create a QMainWindow windowwindow = QMainWindow()
("QMainWindow Example")

# Set the central controlcentral_widget = QLabel("This is a QMainWindow")
(central_widget)

# Add status barstatus_bar = QStatusBar()
status_bar.showMessage("This is a status bar")
(status_bar)

()
()

3. Major differences and comparison

characteristic QWidget QMainWindow
Class level Basic class, parent class of all controls Subclass of QWidget, advanced implementation of main window
Built-in layout none Provide menu bar, toolbar, status bar, etc.
Applicable scenarios Simple window or nested container The main window of the application
Central control support unnecessary Use the setCentralWidget() method
Complexity Lower, lightweight Higher, suitable for complex applications
Extensibility Flexible, manual layout required Provides more features for rapid development

4. Select a suggestion

  • useQWidget

    • If your window is a simple dialog or nested child widget.
    • It requires a complete custom layout and does not require advanced features such as menu bars and toolbars.
  • useQMainWindow

    • If your window is the main interface of an application.
    • Built-in menu bar, toolbar, status bar, docking window and other functions are required.

Summarize

QWidgetIt is the basic widget class, used to build all types of widgets;QMainWindowis an advanced window class suitable for use as the main window of an application. Selecting the right class according to specific needs can complete the development tasks more efficiently.

This is the end of this article about the difference between QWidget and QMainWindow in PyQt6. For more related content on PyQt6, please search for my previous articles or continue browsing the related articles below. I hope you will support me more in the future!