gpt4 book ai didi

c++ - 为什么设置 Qt::SplashScreen 标志阻止关闭

转载 作者:行者123 更新时间:2023-11-30 05:21:02 25 4
gpt4 key购买 nike

我有 QMainWindow,它嵌入了一个 QQuickWidget
QQuickWidget 根据应用程序的状态(初始化或未初始化)显示两个不同的 qml(splash.qmlmain.qml)。

当显示 splash.qml 时,我希望我的窗口处于 splashScreen 模式,所以我这样做了:

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();
}

当初始化完成并加载另一个 qml 文件时,QML 会触发一个槽。然后我将标志重置为其默认值以从启动画面返回:

void MainWindow::UpdateWindowAfterInit()
{
setWindowFlags(mDefaultFlags);
show();
}

一切都按预期进行,但是当我尝试关闭我的应用程序时,它永远不会到达 main() 的末尾,而如果我不应用 Qt::,它会很好地关闭: SplashScreen 标志。

我应该怎么做才能关闭我的应用程序?

最佳答案

首先,让我们试着理解为什么它没有按您预期的那样工作。

通过查看 documentation QWidget::close,我们有以下内容(强调我的):

The QApplication::lastWindowClosed() signal is emitted when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus.

另一方面,我们为 Qt::WA_QuitOnClose 设置了这个:

Makes Qt quit the application when the last widget with the attribute set has accepted closeEvent(). This behavior can be modified with the QApplication::quitOnLastWindowClosed property. By default this attribute is set for all widgets of type Qt::Window.

因此怀疑您设置的属性或您认为设置的属性在您更改标志时实际上被重置了。

通过查看代码,我们有以下内容:

  • HeresetWindowFlags 的实际实现。您可以看到,如果旧类型是一个窗口(也就是说,如果您有 Qt::Window 标志),函数 adjustQuitOnCloseAttribute 被调用设置,这就是你的情况)。

  • HereadjustQuitOnCloseAttribute 的实际实现,并且发生了这种情况:

    // ...

    if (type != Qt::Widget && type != Qt::Window && type != Qt::Dialog)
    q->setAttribute(Qt::WA_QuitOnClose, false);

    // ...

    这意味着当您设置标志 Qt::SplashScreen 时,属性 Qt::WA_QuitOnClose 被设置为 false

最后,对于 Qt::WA_DeleteOnClose,我们有以下内容:

Makes Qt delete this widget when the widget has accepted the close event (see QWidget::closeEvent()).

因为您不再有 Qt::WA_QuitOnClose 集,窗口不再接受 close 事件并且它不会被销毁。

更重要的是,它没有关闭,这就是您在应用程序中观察到的。这不是 Qt 的错误,它是(相当糟糕)记录的预期行为。


现在,我们可以尝试找出解决问题的方法。

可能,以正确的顺序设置正确的标志和属性就足够了。
我不确定,但你可以试一试:

setWindowFlags(Qt::SplashScreen);
setAttribute(Qt::WA_QuitOnClose, true);
setAttribute(Qt::WA_DeleteOnClose, true);

关于c++ - 为什么设置 Qt::SplashScreen 标志阻止关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40406052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com