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 指针时崩溃
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
