Parent-child Qt中成员变量的关系设置
Parent-child relationship set to member variables in Qt
我正在阅读 Qt 文档 here。我在标题'Thread Affinity'.
下找到了下面这句话
Note: A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent().
我不明白它提到的在 object 和它的成员变量之间设置 parent-child 关系。我只知道superclass和subclass之间的parent-child关系。
谁能给我解释一下这句话?如果你能提供一个例子,它会更有帮助。
感谢阅读。
一个 QObject (A
) 可以有其他 QObject (B
's) 作为成员变量。 B
不会自动与 A
一起创建,也不必是。这是可能的,但不是必需的,也不是自动完成的。
不仅class和它的超级class(基础class)之间存在父子关系,而且QWidget和嵌入式小部件之间也存在父子关系。它用于例如最后以正确的顺序销毁小部件。
查看文档 here and an answer on SO regarding memory management here。
如果不显式设置 parent
属性,成员变量将 NOT 成为子对象。 Object
子类通常采用另一个 QObject
作为构造函数中的父类。
class Test : public QObject
{
Q_OBJECT
public:
Test(QObject* prnt)
: QObject(prnt),
timerNoPrnt(), // Test object is NOT the parent. This won't be deleted when Test object gets deleted.
timer(this) // Test object is the parent here. This will be deleted when Test object gets deleted.
{
timerNoPrnt->setParent(this); // now parent assigned.
}
private:
QTimer* timerNoPrnt; // member variable
QTimer* timer;
}
一个 QObject
是一个 object 容器。它包含的 object 被称为它的 children,包含 object 被认为是那些 children 的 parent。这本身并没有说明 children 是如何分配的。让我们看一些场景,在 C++11 中:
void test() {
QObject aParent;
// children of automatic storage duration
QObject aChild1{&aParent}, aChild2;
aChild2->setParent(&aParent);
// children of dynamic storage duration
auto aChild3 = new QObject{&aParent};
auto aChild4 = new QObject;
aChild4->setParent(&aParent);
}
struct Parent : QObject {
QObject aChild5 { this };
QObject * aChild6 { new QObject };
QObject notAChild;
Parent() { aChild6->setParent(this); }
};
test()
函数演示了 object 如何成为 parent 到 children 的自动和动态存储持续时间。 parent object 可以在构造函数中给出,也可以作为 setParent
方法的参数。
Parent
class 表明成员 object 可以是 parent class 的 children,但不一定如此.成员值具有自动存储期限,但并非所有 child object 都是。 aChild6
指向的object是动态存储时长。由于 QObject
在其析构函数中删除了所有 children,因此 object 在 aChild6
的有效存储持续时间是自动的:您不必担心必须删除object.
我正在阅读 Qt 文档 here。我在标题'Thread Affinity'.
下找到了下面这句话Note: A QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent().
我不明白它提到的在 object 和它的成员变量之间设置 parent-child 关系。我只知道superclass和subclass之间的parent-child关系。
谁能给我解释一下这句话?如果你能提供一个例子,它会更有帮助。
感谢阅读。
一个 QObject (A
) 可以有其他 QObject (B
's) 作为成员变量。 B
不会自动与 A
一起创建,也不必是。这是可能的,但不是必需的,也不是自动完成的。
不仅class和它的超级class(基础class)之间存在父子关系,而且QWidget和嵌入式小部件之间也存在父子关系。它用于例如最后以正确的顺序销毁小部件。
查看文档 here and an answer on SO regarding memory management here。
如果不显式设置 parent
属性,成员变量将 NOT 成为子对象。 Object
子类通常采用另一个 QObject
作为构造函数中的父类。
class Test : public QObject
{
Q_OBJECT
public:
Test(QObject* prnt)
: QObject(prnt),
timerNoPrnt(), // Test object is NOT the parent. This won't be deleted when Test object gets deleted.
timer(this) // Test object is the parent here. This will be deleted when Test object gets deleted.
{
timerNoPrnt->setParent(this); // now parent assigned.
}
private:
QTimer* timerNoPrnt; // member variable
QTimer* timer;
}
一个 QObject
是一个 object 容器。它包含的 object 被称为它的 children,包含 object 被认为是那些 children 的 parent。这本身并没有说明 children 是如何分配的。让我们看一些场景,在 C++11 中:
void test() {
QObject aParent;
// children of automatic storage duration
QObject aChild1{&aParent}, aChild2;
aChild2->setParent(&aParent);
// children of dynamic storage duration
auto aChild3 = new QObject{&aParent};
auto aChild4 = new QObject;
aChild4->setParent(&aParent);
}
struct Parent : QObject {
QObject aChild5 { this };
QObject * aChild6 { new QObject };
QObject notAChild;
Parent() { aChild6->setParent(this); }
};
test()
函数演示了 object 如何成为 parent 到 children 的自动和动态存储持续时间。 parent object 可以在构造函数中给出,也可以作为 setParent
方法的参数。
Parent
class 表明成员 object 可以是 parent class 的 children,但不一定如此.成员值具有自动存储期限,但并非所有 child object 都是。 aChild6
指向的object是动态存储时长。由于 QObject
在其析构函数中删除了所有 children,因此 object 在 aChild6
的有效存储持续时间是自动的:您不必担心必须删除object.