qml程序如果存在c++后台线程,那么在关闭qml主窗口时,必须先清理后台线程资源,并退出后台线程之后,才能正常退出程序。否则会报错:QThread: Destroyed while thread is still running
以下是代码:
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
//QGuiApplication app(argc, argv);
QApplication app(argc, argv);
app.setWindowIcon(QIcon(":/app.png"));
//MainBackgroundProcess
QThread threadProcess;
MainBackgroundProcess::instance()->moveToThread(&threadProcess);
QObject::connect(&threadProcess, &QThread::started, [=](){
MainBackgroundProcess::instance()->init();
});
threadProcess.start();
//QMetaObject::invokeMethod(MainBackgroundProcess::instan
在Qt QML应用中,当关闭主窗口时,需要确保正确处理C++后台线程。如果线程仍在运行时销毁,会导致错误:QThread: Destroyed while thread is still running。为避免此问题,应在退出前清理线程资源并安全退出后台线程。
订阅专栏 解锁全文

2796

被折叠的 条评论
为什么被折叠?



