首页 > 解决方案 > Qt:尽管有单独的线程,但程序没有响应

问题描述

我有一个用 C++/Qt 编写的程序来转换某些游戏文件。这是我第一次做这样的事情,所以我还不是特别有经验。我遇到了窗口偶尔显示“未响应...”消息的问题。在阅读之后,问题似乎是处理是在主线程中完成的,阻塞了 gui。所以我尝试为正在完成的实际工作运行一个单独的线程,但是问题仍然存在。

这是创建额外线程的部分:

QThread* thread = new QThread;
WKConverter* converter = new WKConverter(settings);
converter->moveToThread(thread);
connect(converter, SIGNAL(log(std::string)), this, SLOT(log(std::string)));
connect(converter, SIGNAL(setInfo(std::string)), this, SLOT(setInfo(std::string)));
connect(converter, SIGNAL(createDialog(std::string)), this, SLOT(createDialog(std::string)));
connect(converter, SIGNAL(createDialog(std::string, std::string)), this, SLOT(createDialog(std::string, std::string)));
connect(converter, SIGNAL(createDialog(std::string, std::string, std::string)), this, SLOT(createDialog(std::string, std::string, std::string)));
connect(converter, SIGNAL(setProgress(int)), this, SLOT(setProgress(int)));
connect(converter, SIGNAL(increaseProgress(int)), this, SLOT(increaseProgress(int)));

connect(thread, SIGNAL(started()), converter, SLOT(run(bool)));
connect(converter, SIGNAL(finished()), thread, SLOT(quit()));
connect(converter, SIGNAL(finished()), converter, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
converter->run();

使用信号/插槽来推送进度条的更新,以及指示 gui 正在处理的内容的标签。通常这是可行的,但正如我所说我仍然遇到“没有响应......”问题(虽然有趣的是,如果我使用 Qt Creator 进行调试,这似乎不会发生,只有在之后运行编译的 exe 时才会发生)

我错过了什么吗?还有什么我需要做的吗?我会很感激任何指示。

如果有帮助,这里是该项目的 github,尽管我还没有推动在那里完成的线程工作:https ://github.com/Jineapple/WololoKingdoms

标签: c++multithreadingqtqtgui

解决方案


推荐阅读