当前位置:编程学习 > wap >>

QTabWidget中的Widget为什么不能指定父对象?

QTabWidge的文档中是这样描述的:
1.Create a QTabWidget.
2.Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
3.Insert child widgets into the page widget, using layouts to position them as normal.
4.Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.
我不明白第2步中的but do not specify parent widgets for them.
我看一些例子程序,有些指定了,有些没有指定。
1、为什么会有此要求?
2、如果指定了会怎样?
3、不指定时,如果对象是在堆中创建的,它分配的内存要不要自己去回收? --------------------编程问答-------------------- Create a QWidget for each of the pages  为每一页创建一个QWidget,但是不要给这个每一页的子widget指定父widget。
我想应该是默认的就指定好了,指定的就是第一步中创建的QTabWidget。所以就不要再指定了 --------------------编程问答-------------------- 关于第3个问题:“3、不指定时,如果对象是在堆中创建的,它分配的内存要不要自己去回收? ”
我试了一下,不管是不是指定父对象,程序退出时都能调用widget的析构函数,也就是说不要自己去回收。
但还是不明白其道理。还不明白前两个问题。 --------------------编程问答--------------------
引用 2 楼 yearl 的回复:
关于第3个问题:“3、不指定时,如果对象是在堆中创建的,它分配的内存要不要自己去回收? ”
我试了一下,不管是不是指定父对象,程序退出时都能调用widget的析构函数,也就是说不要自己去回收。
但还是不明白其道理。还不明白前两个问题。

add的时候会自动设置parent。
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,