When setting the WA_DeleteOnClose attribute on a Qt MainWindow, the program crashes when deleting the ui pointer(在 Qt MainWindow 上设置 WA_DeleteOnClose 属性时,程序在删除 ui 指针时崩溃)
问题描述
我在 MainWindow 中设置了 WA_DeleteOnClose
小部件属性.
I have set the WA_DeleteOnClose
widget attribute in a MainWindow.
setAttribute(Qt::WA_DeleteOnClose);
但是,每当我关闭该主窗口时,我都会在其析构函数中遇到段错误,该析构函数只有 delete ui;
However, whenever I close that main window, I get a segfault in its destructor, which only has delete ui;
简而言之,在 Creator 中创建了一个 Qt4 GUI 应用程序,将 setAttribute(Qt::WA_DeleteOnClose);
添加到构造函数,程序现在在退出时崩溃.
In a nutshell, created a Qt4 GUI Application in Creator, added the setAttribute(Qt::WA_DeleteOnClose);
to constructor, program now crashes on exit.
推荐答案
你是第一次在析构函数中遇到段错误,还是第二次?请记住,您的主窗口析构函数应该只运行一次.也就是说,它应该运行 either 因为堆栈展开,或 因为 WA_DeleteOnClose
,而不是两者.
Are you getting a segfault in its destructor the first time, or the second time? Remember that your main window destructor should run only once. That is to say that it should run either because of a stack unwind, or because of WA_DeleteOnClose
, not both.
IIRC,Creator 会将主窗口放在 main()
的堆栈上.因此,当 main()
返回时,主窗口被销毁.
IIRC, Creator will put the main window on the stack of main()
. Therefore, when main()
returns the main window is destroyed.
这篇关于在 Qt MainWindow 上设置 WA_DeleteOnClose 属性时,程序在删除 ui 指针时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Qt MainWindow 上设置 WA_DeleteOnClose 属性时,程序在删除 ui 指针时崩溃


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01