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

An icon of a clipboard-list1

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:

  1. 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.

  2. 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.

  3. Create the third button, setting the window as its parent. Add it to the layout, but as local variable.

  4. Create the fourth button as a local variable without setting a parent.

  5. 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

An icon of a clipboard-list1

You need to create two top-level Qt windows and switch a child widget on demand.

To do this:

  1. Create two top-level Qt Windows. On clicking the main window’s ‘Show widgets’ button show two QWidget objects. Don’t set their parents on creation - this makes them top-level windows (if you close the main window, they remain visible and active).

  2. Create the child widget. Add a QLineEdit object to the layout of widget1.

  3. 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 as QWidget, QLabel, or QCheckBox,
  • name: The child’s object name, set via setObjectName(). Defaults to an empty string so it must be set it explicitly for name-based searches (optional).
  • pattern: A QRegularExpression instance for pattern matching.
  • options: Either Qt.FindChildOptions.FindDirectChildrenOnly or Qt.FindChildOptions.FindDirectChildrenRecursively (optional).
An icon of a clipboard-list1

You have a set of checkboxes in a group box and need to check them programmatically based on user input.

To do that:

  1. Create the checkboxes. Instantiate three QCheckBox objects in a QGroupBox and set an object name for each. Also, create a QLineEdit for user input and a button to start the search.

  2. Find the checkboxes. When the button is pressed, use the QLineEdit text to create a QRegularExpression and pass it to findChildren().

  3. 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.

An icon of a clipboard-list1

A “Clear” button lives inside a group box and clears its sibling QLineEdit. You want to move that button to another group box so it clears a different lineedit.

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.

  1. 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)
  2. 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.


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