PYQT5 study notes 05 - QObject parent-child object API and cases

1. Parent-child object API

  Here we briefly demonstrate the specific usage and code implementation of the parent-child object API. There are five parent-child object APIs, namely setParent, parent, children, findChild, and findChildren. Next, we will demonstrate each API in detail.

1、setParent(parent)和parent()

  setParentThe function of the method is to set a parent object for a QObject object, and only one parent object can be set for each object. The role of parent is to get the parent object of an object, the code is as follows:

obj1 = QObject()
obj2 = QObject()
print("obj1", obj1)
print("obj2", obj2)
obj1.setParent(obj2)  # 设置obj1的父对象为obj2
print(obj1.parent())

  The effect is as follows, because the output is a reference, so we can only print the sum first obj1to obj2know who is whose parent object:
insert image description here

  Here is a reminder that a QLabel object cannot set a QObject object as its parent object, and a visible control and an invisible control cannot set a parent-child relationship with each other! ! !

2、children()

  childrenThe role of is to get all of an object 直接子对象, we can first construct a parent-child relationship diagram, the relationship diagram is as follows:
insert image description here

  The construction code is as follows:

        obj0 = QObject()
        obj1 = QObject()
        obj2 = QObject()
        obj3 = QObject()
        obj4 = QObject()
        obj5 = QObject()
        #
        print("obj0", obj0)
        print("obj1", obj1)
        print("obj2", obj2)
        print("obj3", obj3)
        print("obj4", obj4)
        print("obj5", obj5)
        #
        obj1.setParent(obj0)
        obj2.setParent(obj0)


        obj3.setParent(obj1)
        obj4.setParent(obj2)
        obj5.setParent(obj2)

  We can try to output obj4the father and obj0children, the code is as follows:

		print("obj4-parent", obj4.parent())
        print("obj0-child", obj0.children())  # 获取obj0的直接子对象,只有一级

  The result is as follows:
insert image description here

  It is obvious that only obj0the two direct sub-objects of are output.

3. findChild (parameter 1, parameter 2, parameter 3), findChildren (parameter 1, parameter 2, parameter 3)

  findChildThe meaning of the function is to obtain a qualified sub-object among all sub-objects according to the conditions, or to obtain a sub-object with a specified name and type. Even if there are multiple sub-objects that meet the conditions, only one will be output. The meanings of the three parameters are:

参数一是子对象的类型,可以是单个类型变量(QObject)也可以是类型元祖((QPushButton, QLabel))
参数二是子对象的对象名称(或者说是对象ID),可以省略,省略的话就意味着从所有该类型变量中查找,输出查找到的第一个子对象
参数三有两个选择:
	1、Qt.FindDirectChildrenOnly:默认选项,递归查找,在所有子对象中查找
	2、Qt.FindDirectChildrenOnly:只在直接子对象中查找

  findChildrenThe parameters are findChildthe same as , except that it returns all the found sub-objects instead of just one. We can experiment with these two functions:

		print(obj0.findChild(QObject))  # 找到obj0的类型为QObject的子对象,找到一个就结束
        print(obj0.findChildren(QObject))  # 找到obj0的类型为QObject的所有子对象,包括子孙对象

  The effect is as follows:
insert image description here

  We give obj3the setting a name:

        obj3.setObjectName("3")

  Find child objects by name:

print(obj0.findChildren(QObject, "3", Qt.FindChildrenRecursively))  # 找到obj0的类型为QObject,对象名称为3的子对象

The result is as follows:
insert image description here

2. Specific cases

1. Memory management mechanism

  All these controls are actually one tree QObject继承树, and all objects are directly or indirectly inherited from QObject, so QObjects organize themselves in an object tree. When creating a QObject, if another object is used as the parent object, then it will be added to the children list of the parent object. When the parent object is destroyed, this QObject object will also be destroyed. We can demonstrate that in the following code, obj1it is a local variable, which will be released immediately after the function is executed, ojb2but a sub-object of obj1, which is pointed to by obj1 and will not be released automatically. We monitor the release signal of obj2, and when obj2 is Print information when released, the code is as follows:

obj1 = QObject()
obj2 = QObject()

obj2.setParent(obj1)
# 监听obj2对象被释放
obj2.destroyed.connect(lambda : print("obj2对象被释放"))  # 当obj2对象销毁时,执行里面的lambda函数

  The result is as follows:
insert image description here

  So the parent object is destroyed, and the child object is also destroyed.

  For QWidget components, QWidget components extend the parent-child relationship of QObject objects . In QWidget, when a control is set with a parent control, it will be included in the parent control and will be clipped by the area of ​​​​the parent control (that is, the size of the child control cannot exceeds the parent control), the child control is automatically deleted when the parent control is deleted.

  A dialog box with many operation buttons (cancel, ok, etc.), the button and the dialog box itself are parent-child control relationships. When we operate, i is the dialog control itself, not its internal sub-controls (buttons, etc.). When the dialog box is deleted, the internal sub-controls are also automatically deleted.

2. The impact on Qt controls

  Another important function is to set the style of child controls. You can traverse all the child controls of a certain type of parent control, and set the corresponding style for each child control, so you don’t need to set the style one by one. code show as below:

        win_root = QWidget()
        win_root.setWindowTitle("学习python")
        win_root.resize(500, 500)

        label = QLabel(win_root)  # 设置父控件第一种方式
        label.setText("社会我唐哥")
        # label.setStyleSheet("background-color: cyan;")

        label2 = QLabel(win_root)
        label2.move(50, 50)
        label2.setText("人狠话不多")
        # label2.setStyleSheet("background-color: cyan;")

        btn = QPushButton()
        btn.setText("按钮")
        btn.move(100, 100)
        btn.setParent(win_root)  # 设置父控件第二种方式

        for sub_widget in win_root.findChildren(QLabel):  # 给所有QLabel子对象设置背景颜色
            sub_widget.setStyleSheet("background-color: cyan;")

  The effect is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/qq_47188967/article/details/130392841