PyQt QWidget

Summary: in this tutorial, you’ll learn how to use PyQt QWidget as the container of other widgets.

Introduction to the PyQt QWidget

A QWidget is the base class of all other widgets. A QWidget is a blank area to hold other widgets. Therefore, it is useful as a container for organizing child widgets.

The QWidget is like the Frame in Tkinter for hosting other widgets.

The following shows how to create a QWidget object inside the main window or a parent widget:

widget = QWidget(parent)Code language: Python (python)

The parent can be the main window object or parent widget object to which the QWidget object belongs.

Providing the parent widget as an argument of the QWidget ensures that the child widgets are garbage collected when the parent is and limits its visibility to only the parent widget.

PyQt QWidget example

The following program shows how to use the QWidget as the container of other widgets:

import sys
from PyQt6.QtWidgets import QApplication, QWidget,  QLineEdit,  QFormLayout,  QHBoxLayout


class MainWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setWindowTitle('PyQt Widget Demo')

        # create an input pane
        layout = QHBoxLayout()
        self.setLayout(layout)

        # person pane
        person_pane = QWidget(self)
        form_layout = QFormLayout()
        person_pane.setLayout(form_layout)
        form_layout.addRow('First Name:', QLineEdit(person_pane))
        form_layout.addRow('Last Name:', QLineEdit(person_pane))
        form_layout.addRow('Date of Birth:', QLineEdit(person_pane))
        form_layout.addRow('Email Address:', QLineEdit(person_pane))
        form_layout.addRow('Phone Number:', QLineEdit(person_pane))
        layout.addWidget(person_pane)

        # address pane
        address_pane = QWidget(self)
        form_layout = QFormLayout()
        address_pane.setLayout(form_layout)
        form_layout.addRow('Street:', QLineEdit(address_pane))
        form_layout.addRow('City:', QLineEdit(address_pane))
        form_layout.addRow('State/Province:', QLineEdit(address_pane))
        form_layout.addRow('Zip Code:', QLineEdit(address_pane))
        form_layout.addRow('Country:', QLineEdit(address_pane))
        layout.addWidget(address_pane)

        # show the window
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec())Code language: Python (python)

How it works.

First, set the layout of the main window as QHBoxLayout:

layout = QHBoxLayout()
self.setLayout(layout)Code language: Python (python)

Second, create a widget as a container (person_pane) that uses the form layout and adds other widgets to the container:

person_pane = QWidget(self)
form_layout = QFormLayout()
person_pane.setLayout(form_layout)
form_layout.addRow('First Name:', QLineEdit(person_pane))
form_layout.addRow('Last Name:', QLineEdit(person_pane))
form_layout.addRow('Date of Birth:', QLineEdit(person_pane))
form_layout.addRow('Email Address:', QLineEdit(person_pane))
form_layout.addRow('Phone Number:', QLineEdit(person_pane))Code language: Python (python)

Third, create another widget as a container (address_pane) that also uses the form layout and adds other widgets to the container:

address_pane = QWidget(self)
form_layout = QFormLayout()
address_pane.setLayout(form_layout)
form_layout.addRow('Street:', QLineEdit(address_pane))
form_layout.addRow('City:', QLineEdit(address_pane))
form_layout.addRow('State/Province:', QLineEdit(address_pane))
form_layout.addRow('Zip Code:', QLineEdit(address_pane))
form_layout.addRow('Country:', QLineEdit(address_pane))Code language: Python (python)

Finally, add the widget to the main window:

layout.addWidget(person_pane)
layout.addWidget(address_pane)Code language: Python (python)

Summary

  • Use PyQt QWidget as the container of other widgets.
Did you find this tutorial helpful ?