• 2166阅读
  • 7回复

Qt子控件析构的时候,父控件会自动把它从子控件列表中删除吗? [复制链接]

上一主题 下一主题
离线ustone
 

只看楼主 倒序阅读 楼主  发表于: 2018-06-06

比如一个业务类有一个QPushButton对象btn,业务类构造函数中得到主窗体对象指针this,把它设置为btn的parent,并把btn.show出来。当业务类析构的时候(默认析构函数),btn会自动与主窗体对象脱离parent关系吗?
离线stlcours

只看该作者 1楼 发表于: 2018-06-07
如果不脱离关系,父亡,子也不得不亡。所以家国抗战,不是说说的
离线ustone

只看该作者 2楼 发表于: 2018-06-07
回 stlcours 的帖子
stlcours:如果不脱离关系,父亡,子也不得不亡。所以家国抗战,不是说说的 [表情]  (2018-06-07 22:07) 

说啥腻,我说子控件析构,没说父控件销毁。
离线dbzhang800

只看该作者 3楼 发表于: 2018-06-08
当然,会!

这个QObject对象父子关系的基本特性
离线ustone

只看该作者 4楼 发表于: 2018-06-08
回 dbzhang800 的帖子
dbzhang800:当然,会!
这个QObject对象父子关系的基本特性 (2018-06-08 11:13) 

ok,虽然没有讲到原理(why)。
离线stlcours

只看该作者 5楼 发表于: 2018-06-08
回 ustone 的帖子
ustone:说啥腻,我说子控件析构,没说父控件销毁。 (2018-06-07 22:18) 

不是你说的:当业务类析构的时候。。。而且这个业务类拥有btn
离线ustone

只看该作者 6楼 发表于: 2018-06-08
回 stlcours 的帖子
stlcours:不是你说的:当业务类析构的时候。。。而且这个业务类拥有btn (2018-06-08 17:03) 

没说清楚,btn是业务类的成员对象,btn的parent为主窗体。
离线ustone

只看该作者 7楼 发表于: 2018-06-27
看到了:
Construction/Destruction Order of QObjects
When QObjects are created on the heap (i.e., created with new), a tree can be constructed from them in any order, and later, the objects in the tree can be destroyed in any order. When any QObject in the tree is deleted, if the object has a parent, the destructor automatically removes the object from its parent. If the object has children, the destructor automatically deletes each child. No QObject is deleted twice, regardless of the order of destruction.
When QObjects are created on the stack, the same behavior applies.
快速回复
限100 字节
 
上一个 下一个