Skip to content

QMainWindow


A main window provides a framework for building an application's user interface. Every user interface must have a QMainWindow in PySide6. A QMainWindow must have a central widget, which is the main widget in the window. The central widget can be a QWidget or any other widget subclass. To this central widget, you can add other layouts, such as QHBoxLayout and QVBoxLayout.


Methods


Examples


class UserInterface(QtWidgets.QMainWindow):
    def __init__(self):

        # Call the QMainWindow __init__ method.
        super().__init__()

        # Set the central widget; every QMainWindow must have a central widget.
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(QWidget())

        # Add a layout to the central widget.
        layout = QtWidgets.QVBoxLayout(central_widget)

def main():

    # Create the application object.
    app = QtWidgets.QApplication()

    # Create the main window, show it, and start the event loop.
    window = UserInterface()
    window.show()
    app.exec()
In the above example, a simple user interface is created using the QMainWindow class. We create a class called UserInterface that inherits from QMainWindow. We then call the __init__ method of the QMainWindow class using the super() function. We then create a central widget and set it as the central widget of the main window and add a layout to the central widget. Finally, we create an instance of the UserInterface class, show the main window, and start the application event loop.
Running the main function in the above code will display a window with a vertical layout.



setCentralWidget(widget)

Sets the given widget to be the main window's central widget.

Parameters:

Name Type Description Default
widget QWidget

The widget to set as the central widget.

required


show()

Shows the main window.