我注意到在http://doc.qt.io/qt-5/qthread.html#details的示例中,线程不应真正终止,因为工作程序的函数没有调用quit()或引发被评估的信号(并调用quit()),因此内存将不会被释放。这是一个错误吗?
我写了一个稍作修改的演示。这是正确的还是有更好的方法?
class Example : public QObject
{
Q_OBJECT
Q_SIGNALS:
void finished();
public:
void worker() {
// Do the job
emit finished();
}
};
Example *tmp = new Example;
tmp->moveToThread(&_thread);
connect(tmp, &PoserEngine::finished, &_thread, &QThread::quit);
connect(&_thread, &QThread::finished, tmp, &QObject::deleteLater);
connect(&_thread, &QThread::started, tmp, &PoserEngine::worker);