GUI应用程序退出时退出QThread [英] Exit QThread when GUI Application exits
问题描述
我有以下工人阶级:
class MediaWorker : public QObject
{
Q_OBJECT
public:
explicit MediaWorker(QObject *parent = 0);
~MediaWorker();
void Exit();
signals:
void Finished();
public slots:
void OnExecuteProcess();
};
在MediaWorker.cpp中
In MediaWorker.cpp
void MediaWorker::Exit()
{
emit Finished();
}
void MediaWorker::OnExecuteProcess()
{
qDebug() << "Worker Thread: " << QThread::currentThreadId();
}
在MainWindow中,请执行以下操作:
In my MainWindow I do the following:
this->threadMediaWorker = new QThread();
this->mediaWorker = new MediaWorker();
this->timerMediaWorker = new QTimer();
this->timerMediaWorker->setInterval(1000);
this->timerMediaWorker->moveToThread(this->threadMediaWorker);
this->mediaWorker->moveToThread(this->threadMediaWorker);
connect(this->threadMediaWorker, SIGNAL(started()), this->timerMediaWorker, SLOT(start()));
connect(this->timerMediaWorker, &QTimer::timeout, this->mediaWorker, &MediaWorker::OnExecuteProcess);
connect(this->mediaWorker, &MediaWorker::Finished, this->threadMediaWorker, &QThread::quit);
connect(this->mediaWorker, &MediaWorker::Finished, this->mediaWorker, &MediaWorker::deleteLater);
connect(this->threadMediaWorker, &QThread::finished, this->mediaWorker, &QThread::deleteLater);
this->threadMediaWorker->start();
线程正常工作.当我关闭应用程序时,我终止了析构函数中的线程:
The threading is working properly. When I close the application I terminate the thread in the destructor:
MainWindow::~MainWindow()
{
delete ui;
this->mediaWorker->Exit();
}
因此Exit()发出Finished信号,有望删除qthread和mediaworker类.
so Exit() emits the Finished signal which will hopefully delete the qthread and mediaworker class.
我的问题是,这是否是终止线程和媒体工作者类的正确方法?
My question is if this is the proper way of terminating both the thread and media worker class?
推荐答案
我的问题是,终止两者的正确方法是什么 工作线程和媒体工作程序类?
My question is what is the proper way of terminating both the worker thread and media worker class?
您只需使用QScopedPointer
或std::unique_ptr
来确保在删除主窗口的同时删除了媒体"对象.
You can just ensure that the 'media' object gets deleted while the main window gets destructed by using either QScopedPointer
or std::unique_ptr
.
class MainWindow : public QMainWindow {
/// ...
QThread m_workerThread;
QScopedPointer<MediaWorker> m_pMediaObject;
/// ...
};
void MainWindows::init()
{
// ... other initialization skipped ...
// for dynamic allocation of the object and keeping the track of it
m_mediaObject.reset(new MediaWorker());
m_workerThread.moveToThread(m_mediaObject.data());
}
void MainWindow::stopWorker()
{
if (m_workerThread.isRunning())
{
m_workerThread.quit(); // commands Qt thread to quit its loop
// wait till the thread actually quits
m_workerThread.wait(); // possible to specify milliseconds but
// whether or not to limit the wait is
// another question
}
}
如果工作线程用于更新UI,则在
If the worker thread used for updating the UI it makes sense to attempt to stop the worker thread before the UI objects released in
void MainWindow::closeEvent(QCloseEvent *)
{
stopWorker();
}
但是主窗口有可能永远不会在销毁之前被调用closeEvent()
,因此我们应该进行处理:
But there is a chance that the main window never gets closeEvent()
called before the destruction so we should handle that:
MainWindow::~MainWindow()
{
stopWorker();
// it also destroys m_mediaObject
}
这篇关于GUI应用程序退出时退出QThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!