C++ Qt:如何正确使用moveToThread(这个)?
我有一门课是这样的:C++ Qt:如何正确使用moveToThread(这个)?,c++,multithreading,qt,C++,Multithreading,Qt,我有一门课是这样的: class myClass:public QThread 然后在它的构造函数中我做了: myClass::myClass(){ moveToThread(this); ... } 看起来所有的成员插槽实际上都在工作线程上工作。 但是在这种情况下,在解构过程中,我如何停止线程呢?就是不要这样做线程 Qt文档中介绍了使用moveToThread()的正确方法: class Worker : public QObject { Q_OBJECT pub
class myClass:public QThread
然后在它的构造函数中我做了:
myClass::myClass(){
moveToThread(this);
...
}
看起来所有的成员插槽实际上都在工作线程上工作。
但是在这种情况下,在解构过程中,我如何停止线程呢?就是不要这样做线程 Qt文档中介绍了使用
moveToThread()
的正确方法:
class Worker : public QObject
{
Q_OBJECT
public slots:
void doWork(const QString ¶meter) {
QString result;
/* ... here is the expensive or blocking operation ... */
emit resultReady(result);
}
signals:
void resultReady(const QString &result);
};
class Controller : public QObject
{
Q_OBJECT
QThread workerThread;
public:
Controller() {
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::doWork);
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
~Controller() {
workerThread.quit();
workerThread.wait();
}
public slots:
void handleResults(const QString &);
signals:
void operate(const QString &);
};
或者通过继承QThread
,例如:
class WorkerThread : public QThread
{
Q_OBJECT
void run() Q_DECL_OVERRIDE {
QString result;
/* ... here is the expensive or blocking operation ... */
emit resultReady(result);
}
signals:
void resultReady(const QString &s);
};
void MyObject::startWorkInAThread()
{
WorkerThread *workerThread = new WorkerThread(this);
connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults);
connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
workerThread->start();
}
但不是两者同时存在。
关于该主题的更多信息,请不要这样做 Qt文档中介绍了使用
moveToThread()
的正确方法:
class Worker : public QObject
{
Q_OBJECT
public slots:
void doWork(const QString ¶meter) {
QString result;
/* ... here is the expensive or blocking operation ... */
emit resultReady(result);
}
signals:
void resultReady(const QString &result);
};
class Controller : public QObject
{
Q_OBJECT
QThread workerThread;
public:
Controller() {
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Controller::operate, worker, &Worker::doWork);
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
workerThread.start();
}
~Controller() {
workerThread.quit();
workerThread.wait();
}
public slots:
void handleResults(const QString &);
signals:
void operate(const QString &);
};
或者通过继承QThread
,例如:
class WorkerThread : public QThread
{
Q_OBJECT
void run() Q_DECL_OVERRIDE {
QString result;
/* ... here is the expensive or blocking operation ... */
emit resultReady(result);
}
signals:
void resultReady(const QString &s);
};
void MyObject::startWorkInAThread()
{
WorkerThread *workerThread = new WorkerThread(this);
connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults);
connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
workerThread->start();
}
但不是两者同时存在。
有关该主题的更多信息请看这里:请看这里: