Skip to content

QWidget


The QWidget class is the base class of all user interface objects in PySide6.QtWidgets.

Parameters:

Name Type Description Default
parent QWidget

The parent widget of this widget.

None

Methods


Examples


widget = QtWidgets.QWidget()
widget.resize(250, 150)

# QPushButton is a subclass of QWidget
button = QtWidgets.QPushButton("press me")
button.resize(100, 50)
In the above code we show that we can apply the resize method to all widgets that inherit from QWidget such as QPushButton. Another example with QMainWindow is shown below:

# QApplication is required to run the application
app = QtWidgets.QApplication()

# QMainWindow is a subclass of QWidget
window = QtWidgets.QMainWindow()
window.resize(500, 300)

# Displays an empty window with a size of 500x300 pixels
window.show()
app.exec()


The following example shows two methods to set a layout manager for a QWidget. The two methods are equivalent.

# Create a main window with a central widget
window = QtWidgets.QMainWindow()
widget = QtWidgets.QWidget()
window.setCentralWidget(widget)

# method 1
layout = QtWidgets.QVBoxLayout()
widget.setLayout(layout)

# method 2
layout = QtWidgets.QVBoxLayout(widget)



resize(w, h)

Resizes the widget to have a width of w pixels and a height of h pixels.

Parameters:

Name Type Description Default
w int

The width of the widget in pixels.

required
h int

The height of the widget in pixels.

required


setLayout(layout)

Sets the layout manager for this widget. An alternative to calling this function is to pass this widget to the layout's constructor, as shown in the last example above.

Parameters:

Name Type Description Default
layout QLayout

The layout manager to set for this widget.

required


show()

Shows the widget and its child widgets.