我想創建一個處理這個特定類的所有事件的Scanner類的線程,從而釋放GUI線程。我的GUI上有一個退出按鈕,它只是簡單地調用qApp-> quit()來退出應用程序,但我不知道如何處理我的Scanner類中的線程。在退出應用程序時,我在調試日誌中看到以下錯誤。退出應用程序安全退出Qt線程
QThread::wait: Thread tried to wait on itself
QThread::wait: Thread tried to wait on itself
QThread: Destroyed while thread is still running
在Scanner.cpp(略其他功能)
Scanner::Scanner() :
{
this->moveToThread(&m_thread);
connect(&m_thread, &QThread::finished, this, &QObject::deleteLater);
connect(this, SIGNAL(StartEnroll()), this, SLOT(StartEnrollment()));
m_thread.start();
}
Scanner::~Scanner()
{
m_thread.quit(); // Not sure if this is the correct
m_thread.wait();
}
在主Window.cpp(略其他功能)
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->ExitButton, SIGNAL(released()), this, SLOT(Quit()));
connect(&m_scanner, SIGNAL(FinishedEnroll(bool)), this, SLOT(EnrollDone(bool)));
}
void MainWindow::Quit()
{
close();
qApp->quit();
}
關於如何退出安全地退出該應用程序的任何指針在多線程應用程序中。
這沒有奏效。我仍然得到:QThread :: wait:線程試圖自己等待 QThread:在線程仍在運行時被銷燬 – GMahan 2015-04-03 14:00:03
Scanner類不應該試圖退出線程或等待線程。工作線程應該由GUI線程擁有和控制。 – RobbieE 2015-04-03 14:03:05
謝謝,根據您的回覆發佈我的更正解決方案。 – GMahan 2015-04-03 14:47:29