When setting the WA_DeleteOnClose attribute on a Qt MainWindow, the program crashes when deleting the ui pointer

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 16:46:09

问题


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:


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.




回答2:


this link gives a good suggestion on solution.

Me I think the best is to define as QPointer to point to the obj and test NULL each time using it, so when the UI obj get destroyed, the pointer set to NULL directly. "QPointer provides guarded pointers for QObjects. You can use it to hold a reference to your dialog and when the dialog is deleted the pointer will be set to NULL automatically."



来源:https://stackoverflow.com/questions/3153155/when-setting-the-wa-deleteonclose-attribute-on-a-qt-mainwindow-the-program-cras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!