I have QMainWindow which embed a QQuickWidget.
The QQuickWidget display two differents qml (splash.qml and main.qml) according to the state of the app (initialized or not).
I want my window to be in splashScreen mode when the splash.qml is displayed, so I did :
MainWindow::MainWindow(QMainWindow * parent) :QMainWindow(parent)
{
QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
mDefaultFlags = windowFlags();
setAttribute(Qt::WA_DeleteOnClose, true);
setWindowFlags(Qt::SplashScreen);
mQuickWidget = new QQuickWidget(this);
//...
setCentralWidget(mQuickWidget);
mQuickWidget->show();
}
The QML trigger a slot when the init is done and the other qml file is loaded. I then reset the flags to their default value to go back from splashscreen :
void MainWindow::UpdateWindowAfterInit()
{
setWindowFlags(mDefaultFlags);
show();
}
Everything goes as expected , but when I try to close my app it never reach the end of the main() whereas it close nicely if i don't apply the Qt::SplashScreen flag.
What should I do to be able to close my app ?
First of all, let's try to understand why it doesn't work as you expect.
By looking at the documentation of
QWidget::close, we have the following (emphasis mine):On the other side, we have this for
Qt::WA_QuitOnClose:Therefore the suspect is that attributes you set or you think are set are actually reset when you change the flags.
By looking at the code, we have the following:
Here is the actual implementation of
setWindowFlags. You can see that the functionadjustQuitOnCloseAttributeis invoked if the old type was a window (that is, if you had theQt::Windowflag set and that's your case).Here is the actual implementation of
adjustQuitOnCloseAttributeand this happens:This means that attribute
Qt::WA_QuitOnCloseis set tofalsewhen you set the flagQt::SplashScreen.Finally, we have the following for
Qt::WA_DeleteOnClose:For you don't have anymore the
Qt::WA_QuitOnCloseset, the window no longer accepts thecloseevent and it is not destroyed.More important, it is not closed, that is what you are observing in your application. This is not a bug of Qt, it is a (quite badly) documented intended behavior.
Now, we can try to figure what to do to solve the problem.
Probably, it's suffice to set the right flags and attributes in the right order to work around it.
I'm not sure about that, but you can give it a try: