class worker : public QObject{
Q_OBJECT
public:
worker(QObject* parent = NULL);
~worker(){}
Q_SLOT void process();
Q_SLOT void stop();
private:
bool quit;
};
worker::worker(QObject *parent) : QObject(parent){
quit = false;
}
void worker::process(){
while(!quit){
//this library call puts the thread to sleep until a frame is available
WaitForVideoFrame();
}
}
void worker::stop(){
quit = true;
}
然后从UI对象中,我有:
MyWorker = new worker();
QThread* thread = new QThread;
MyWorker->moveToThread(thread);
connect(thread, SIGNAL(started()), MyWorker, SLOT(process()));
QPushButton* stop_button = new QPushButton(this);
connect(stop_button, SIGNAL(clicked(bool)), MyWorker, SLOT(stop()));
thread->start();
这里的问题是当我按下stop_button时,没有任何反应,worker仍在运行循环。也许有一个函数可以从无限循环中调用以将处理时间让给事件循环?或者有更好的设计/解决方案吗?欢迎提出任何建议。