我知道,当一个QObject http://doc.qt.io/qt-5/qobject.html被摧毁了,它也摧毁了它所有的孩子。但是,在我最近的所有 Qt 代码中,我总是声明任何类的成员(例如,QMainWindow https://doc.qt.io/qt-5/qmainwindow.html)作为直接成员,而不是指针成员,这使得 Qt 的内存管理很少(如果有的话)有用。
So, is this parenting property of QObjects http://doc.qt.io/qt-5/qobject.html used for something else? From the documentation, it isn't clear if QBoxLayout::addWidget() https://doc.qt.io/qt-5/qboxlayout.html#addWidget takes ownership of the widget (I think it doesn't). (When you add widgets to a layout, their parent will be set to the parent of the layout http://doc.qt.io/qt-5/layout.html#tips-for-using-layouts, as thuga https://stackoverflow.com/users/2257050/thuga, pointed out).
我想到了其他用途:
- 使用时
moveToThread()
,整个对象树都被移动,而不仅仅是推送的对象(否则,内存管理变得不可能);
-
QWidget
extends the parent/child relation with new semantics, for instance:
- 子部件的坐标是相对于父部件的;
- 父部件会将其子部件剪裁到其自己的几何形状;
- 同级部件的顺序决定了它们的 z 顺序(堆叠);
- 未处理的鼠标/键盘事件在层次结构中向上传播;
- 顶级小部件的父级将使顶级共享相同的任务栏条目,并显示在父级顶部的中心(典型情况:对话框)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)