Reputation: 33605
I'm experimenting with the WA_DeleteOnClose
attribute, which implements exactly what I need. Much more neat than doing the same manually. There's a problem, however: if I construct my QMainWindow
that WA_DeleteOnClose
is applied to with no parent, it doesn't get deleted on close. And if I do set a parent (the main QMainWindow
of the application), the secondary QMainWindow
in question loses its taskbar button, which is unacceptabe. How can I solve this dilemma?
Upvotes: 1
Views: 1671
Reputation: 98505
The WA_DeleteOnClose
should work for any top-level widget. If it doesn't, it's a bug, or you're doing something else wrong, like perhaps running a nested event loop. A simple test for whether the window gets deleted would be:
QObject::connect(widget, &QObject::destroyed, [](QObject * obj){
qDebug() << obj << "was destroyed";
});
WA_DeleteOnClose is applied to with no parent, it doesn't get deleted
There's no code in the implementation of WA_DeleteOnClose
behavior that is affected by the widget having a parent. As long as the widget is-a Qt::Window
, it will be deleted. See closeHelper
.
the secondary QMainWindow in question loses its taskbar button, which is unacceptable
This probably applies to any QWidget
, not only QMainWindow
.
To confirm, does this test case not work for you? Let us know what the application output shows.
#include <QLabel>
#include <QPointer>
#include <QApplication>
int main(int argc, char ** argv) {
QApplication app(argc, argv);
QPointer<QLabel> label = new QLabel("Hello, world");
label->setAttribute(Qt::WA_DeleteOnClose);
label->setAttribute(Qt::WA_QuitOnClose);
label->show();
app.exec();
qDebug() << qVersion() << label; // will be null if label was deleted
return 0;
}
Upvotes: 1