【发布时间】:2016-06-29 15:00:03
【问题描述】:
我想要一个只要在我的程序中触发信号就休眠的线程。 我创建了一个 Qthread 并将我的类添加到其中
QThread* serialthread = new QThread;
Serial* serial = new Serial();
serial->moveToThread(serialthread);
connect(serial, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(serialthread, SIGNAL(started()), serial, SLOT(process));
connect(serial, SIGNAL(finished()), serialthread, SLOT(quit()));
connect(serial, SIGNAL(finished()), serial, SLOT(deleteLater()));
connect(serialthread, SIGNAL(finished()), serialthread, SLOT(deleteLater()));
serialthread->start();
我类的函数serial::sendRequest() 应该由信号触发。 另一次我希望线程在不浪费 CPU 时间的情况下休眠。
【问题讨论】:
-
只需将 slot
sendRequest()连接到一个信号就可以了。您的具体问题是什么? -
我的问题是不知道 qthread::run 启动了一个不会烧毁 CPU 的事件循环。但现在我知道了:)
标签: c++ multithreading qt qthread