Qtwebengine进程在应用程序关闭后没有关闭

Qtwebengineprocess is not closed after application closing

我有一个使用 Qt WebEngine 的应用程序。但我发现在关闭我的应用程序或崩溃后 "Qtwebengineprocess" 仍然存在。我的应用程序太大,无法在此处显示,但这里有一个小示例也可以说明问题:

#include <QApplication>
#include <QWebEngineView>
#include <QProcess>
#include <QTimer>


int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QWebEngineView* viewer = new QWebEngineView(NULL);
    viewer->show();
    viewer->load(QUrl("https://www.telegraph.co.uk/content/dam/Pets/spark/royal-canin/tabby-kitten-small.jpg?imwidth=1400"));
    QTimer::singleShot(2000, []() {
        exit(-1);
    });
    app.exec();
    delete viewer;

    return 0;
}

我是不是忘记设置了什么?或者这是一个 Qt 错误?提前致谢。

UPD: Qt 5.11, Win10

参考this post,当在main()中调用exit()时,不会为局部范围的对象调用析构函数! exit() 没有 return.

放置在 app.exec() 之后的任何代码(在您的情况下 delete viewer;),将仅在主事件循环 quits/exits 和 returns 之后执行到调用者,您的计时器正在从主循环内调用 (stdlib) exit(),这意味着:您正在退出执行而没有 return 调用者,并且,放置在 app.exec() 之后的代码的 none 将是执行,如果你想让你的代码正确地 运行 并执行 delete viewer;,那么计时器应该退出主事件循环,因此你需要调用 app.quit()app.exit(-1).

这似乎是 PyQt 5.11 及更高版本中的错误。重新安装我的 OS 并安装最新版本的 PyQt (5.11.3) 后,我遇到了 QWebEngineView 在布局中无法正确调整大小的问题和其他问题。降级到 PyQt 5.10.1 并且一切 运行 再次正常。如果使用 Python,只需 运行:

pip uninstall PyQt5
pip install PyQt5==5.10.1

我找到了实际问题和解决方案 - here。这是准确描述此问题的 Qt 5.11 错误。

其中一条评论有适合我的解决方案:

When running with QCoreApplication::setAttribute(Qt::AA_UseOpenGLES); 
at the top of the main() function, I'm observing that the qtwebengine process closes correctly, 
both when stopping the debugger and when the release exe crashes.

刚刚在创建我的 qApp 之前添加了该行,没有发现任何崩溃。当然,这伴随着在 Qt 上使用 ANGLE 与动态 GPU 的优点和缺点,more details here