Qtwebengine进程在应用程序关闭后不会关闭

Qtwebengineprocess is not closed after application closing

本文关键字:进程 应用程序 Qtwebengine      更新时间:2023-10-16

我有一个使用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错误?提前谢谢。

更新:Qt 5.11, Win10

我找到了实际的问题和解决方案 - 在这里.这是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有好处也有缺点,更多细节在这里。

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

pip uninstall PyQt5
pip install PyQt5==5.10.1

参考这篇文章,当exit()main()中被调用时,不会为本地范围的对象调用析构函数!exit()不会返回。

放置在app.exec()之后的任何代码(在您的情况下delete viewer;),只有在主事件循环退出/退出并返回给调用者后才会执行, 您的计时器正在从主循环中调用 (stdlib)exit(),这意味着: 您正在退出执行而不返回调用者, 并且,放置在app.exec()之后的任何代码都不会被执行, 如果您希望代码正常运行并执行delete viewer;,那么计时器应该退出主事件循环,因此您需要调用app.quit()app.exit(-1)