17. Object Trees and Ownership
QObjects are organized in trees: when you create an object with another object as a parent, the child is added to the parent’s children() list and automatically deleted when the parent is1. There are several ways to set an object’s parent:
- Creating an object by passing the parent to its
__init__()method. - Using
QObject.setParent(). This methods allows changing an object’s parent after creation. - Adding a widget to a layout. When adding a widget to a
QLayout, the layout automatically reparents it to the layout’s owning widget - Setting a layout on a widget. Assigning a layout using
setLayout()makes the widget the parent of any widgets in the layout.
Note that Python objects also form tree hierarchies, but a Python parent-child relationship doesn’t imply Qt a Qt one.
17.1 Parent-Child Relationships
![]() |
You are tasked with creating five Qt push buttons and displaying them in a window. |
You create five QPushButton instances in the window’s __init__() method:
Create the first button and set the window as its parent. Omit adding it to the layout or as an instance member, leaving it as a local variable.
Create the second button without setting a parent. Add it to the layout (which reparents it to the window), but keep it as a local variable.
Create the third button, setting the window as its parent. Add it to the layout, but as local variable.
Create the fourth button as a local variable without setting a parent.
Create the fifth button as an instance member of the window class but without setting a Qt parent.

The program output is:
1 Button 1 parent: Main Window
2 Button 2 parent: Main Window
3 Button 3 parent: Main Window
4 Button 4 parent: None
5 Button 5 parent: None
6
7 Window members:
8 button5

The first three buttons become the windows child widgets - directly or via the layout - and appear on the screen. The first button displays in the top-left corner since it is not in the layout. None of these three buttons are instance members of the Window Python object.
The fourth button is neither part of the Qt object hierarchy nor a Python object member, so it goes out of scope (and is garbage-collected) when __init__() returns, and is never shown.

The fifth button is an instance member of the window object (preventing garbage collection) but not part of the Qt object hierarchy, so it is not shown in the window.
17.2 Reparenting Qt Objects
![]() |
You need to create two top-level Qt windows and switch a child widget on demand. |
To do this:
Create two top-level Qt Windows. On clicking the main window’s ‘Show widgets’ button show two
QWidgetobjects. Don’t set their parents on creation - this makes them top-level windows (if you close the main window, they remain visible and active).Create the child widget. Add a
QLineEditobject to the layout ofwidget1.On clicking the ‘Switch parent’ button reparent the line edit. Remove it from the current widget’s layout, use
setParent()to assign the other widget as its parent, and add it to its layout for visibility. The line edit displays its current parent’s object name.

17.3 Finding Qt Object Children
Qt lets you find an object’s children using these QObject methods:
findChild(type[, name[, options]]): Finds a single matching child.findChildren(type, pattern[, options]): Finds multiple children by exact name.findChildren(type[, name[, options]]): Finds multiple children by regex pattern,
with these parameters:
type: A PySide6 class such asQWidget,QLabel, orQCheckBox,name: The child’s object name, set viasetObjectName(). Defaults to an empty string so it must be set it explicitly for name-based searches (optional).pattern: AQRegularExpressioninstance for pattern matching.options: EitherQt.FindChildOptions.FindDirectChildrenOnlyorQt.FindChildOptions.FindDirectChildrenRecursively(optional).
![]() |
You have a set of checkboxes in a group box and need to check them programmatically based on user input. |
To do that:
Create the checkboxes. Instantiate three
QCheckBoxobjects in aQGroupBoxand set an object name for each. Also, create aQLineEditfor user input and a button to start the search.Find the checkboxes. When the button is pressed, use the
QLineEdittext to create aQRegularExpressionand pass it tofindChildren().Update the checkboxes. Iterate over the found checkboxes and set each one’s check state to
Checked.

17.4 Manual Ownership Transfer
Sometimes you want to dynamically reconfigure a UI by moving a widget from one container to another - complete with correct behavior in its new location.
![]() |
A “Clear” button lives inside a group box and clears its sibling |
We have two groupboxes, each containing a QlineEdit. A single “Clear” button starts in Group 1 and clear its sibling lineedit. Clicking “Move the Button” transfers the button to the other groupbox, and you must set it to clear its new sibling.
-
Update state and connections. Moving a widget does not affect existing signal-slot connections. Our button is still connected to the old lineedit’s
clear()slot. We have to:- Disconnect the old connection
- Connect to the new lineedit
- Update state (enable/disable the appropriate lineedit)
Transfer ownership. There is no need remove the button from its original layout or use
setObject()to change its parent, as adding it to the new layout takes care of the both.
Now if you move the Clear button from one groupbox to another, it will clear its sibling lineedit text correctly.

If you don’t implement
__init__()in your class at all, the parent class gets initalized automatically.↩︎
