When setting the WA_DeleteOnClose attribute on a Q

2019-02-13 14:22发布

I have set the WA_DeleteOnClose widget attribute in a MainWindow.

setAttribute(Qt::WA_DeleteOnClose);

However, whenever I close that main window, I get a segfault in its destructor, which only has delete ui;

In a nutshell, created a Qt4 GUI Application in Creator, added the setAttribute(Qt::WA_DeleteOnClose); to constructor, program now crashes on exit.

1条回答
女痞
2楼-- · 2019-02-13 14:59

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 will put the main window on the stack of main(). Therefore, when main() returns the main window is destroyed.

查看更多
登录 后发表回答