在 Qt MainWindow 上设置 WA_DeleteOnClose 属性时,程序在删除 ui 指针时崩溃

When setting the WA_DeleteOnClose attribute on a Qt MainWindow, the program crashes when deleting the ui pointer(在 Qt MainWindow 上设置 WA_DeleteOnClose 属性时,程序在删除 ui 指针时崩溃)

本文介绍了在 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 指针时崩溃

基础教程推荐