QT 子窗体析构问题

bj-alex 2017-10-12 04:36:00
有一个父窗体和子窗体,都是widget。
我在父窗体类的private里 实例化一个子窗体,如:CcompA my_compA; 没有用new.
现在子窗体中使用checkbox控件,程序执行,选中checkbox,用close()函数关闭子窗体,当再次打开子窗体时,该控件还是选中的状态。
是不是因为close(),没有析构子窗体造成的?
如果我想关闭子窗体时,同时析构子窗体类,应该怎么做?
...全文
1339 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qc50572510 2017-12-08
  • 打赏
  • 举报
回复
楼主是怎么解决的?我也遇到了类似的问题,求指教
Acuity. 2017-10-13
  • 打赏
  • 举报
回复
Qt内存自动释放有两个前提条件: 1. 必须是QObject的派生类; 2. 必须指定了parent对象 。 即:Qt 中 父亲被删除的时候会自动销毁他的孩子。 所以,如果New出来的并且没有父亲,那么则需要手动删除它。
bj-alex 2017-10-13
  • 打赏
  • 举报
回复
引用 4 楼 maguiwa 的回复:
你在子窗体的析构函数中设个断点看是否析构了,还有close()是关闭窗体,destory()是销毁窗体。按照字面理解理解。
widget窗口,用close()关闭,本身并不析构。而且我在父窗体的private里实例化子窗体,没用new。所以,除非父窗体关闭,否则子窗体应该不会析构。 我困惑的是,为什么关闭了窗体,重新打开子窗体,checkbox的选中状态没变化。而且,使用setchecke()函数修改无效。
bj-alex 2017-10-13
  • 打赏
  • 举报
回复
引用 5 楼 qqwangfan 的回复:
bool QWidget::close() Closes this widget. Returns true if the widget was closed; otherwise returns false. First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it ignores the event, nothing happens. The default implementation of QWidget::closeEvent() accepts the close event. If the widget has the Qt::WA_DeleteOnClose flag, the widget is also deleted. A close events is delivered to the widget no matter if the widget is visible or not. The QApplication::lastWindowClosed() signal is emitted when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus. 可以调一下qwidget的void setWindowFlags(Qt::WindowFlags type)函数设置一下窗口的Qt::WA_DeleteOnClose标志,就会在你调用close()后销毁窗体。
我在子窗体的构造函数里添加了属性,setAttribute(Qt::WA_DeleteOnClose,true),因为是子窗体不是new出来的,我不知道用这个方法对不对。而且我用这个方法时,系统报错。程序崩溃了。
bj-alex 2017-10-13
  • 打赏
  • 举报
回复
搞定了,因为是多个控件,我用了复制。
酱油党 2017-10-12
  • 打赏
  • 举报
回复
你在子窗体的析构函数中设个断点看是否析构了,还有close()是关闭窗体,destory()是销毁窗体。按照字面理解理解。
  • 打赏
  • 举报
回复
bool QWidget::close() Closes this widget. Returns true if the widget was closed; otherwise returns false. First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it ignores the event, nothing happens. The default implementation of QWidget::closeEvent() accepts the close event. If the widget has the Qt::WA_DeleteOnClose flag, the widget is also deleted. A close events is delivered to the widget no matter if the widget is visible or not. The QApplication::lastWindowClosed() signal is emitted when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus. 可以调一下qwidget的void setWindowFlags(Qt::WindowFlags type)函数设置一下窗口的Qt::WA_DeleteOnClose标志,就会在你调用close()后销毁窗体。
未狂 2017-10-12
  • 打赏
  • 举报
回复
写析构函数 Qt智能销毁控件,可以用deletelater
bj-alex 2017-10-12
  • 打赏
  • 举报
回复
http://www.qtcn.org/bbs/simple/?t60409.html 这里的问题跟我的很相似。有一句“如果是在栈上建立的,直接关联destroyed信号就可以”,这个关联destroyed信号我不明白。如果按照这个方法,应该可以解决我的问题。
bj-alex 2017-10-12
  • 打赏
  • 举报
回复
引用 1 楼 shihoongbo 的回复:
写析构函数 Qt智能销毁控件,可以用deletelater
其实可以不析构,只是,我不明白为什么checkbox选中状态不改变。我用setchecked(false)没效果。

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧