Since I have a worker class that has 2 slots: StartWork () and StopWork (), then StartWork () alone runs an infinite loop (it just reads and reads the input camera non-stop) and the StopWork () method simply sets the bool variable to false (therefore, the loop inside StartWork () stops).
according to the QThread documentation, the best way to use them now is not to subclass, but to move workers to a thread, so what I do. the problem is that the start () signal from the stream is called, but the completed () signal is never called.
working class slots:
void StartWork () {running = true; while (works) {do work; }}
void StopWork () {running = false; }
QThread initialization and signal / slot connection:
thread = new QThread(); worker = new Worker(); worker.moveToThread(thread); QObject::connect(thread, SIGNAL(started()), worker, SLOT(StartWork())); QObject::connect(thread, SIGNAL(finished()), worker, SLOT(StopWork()));
and on my QPushButton I do this:
if(pushStartStop->text().toLower() == "start") { pushStartStop->setText("Stop"); thread->start(); } else { pushStartStop->setText("Start"); thread->quit(); }
threaded> start () works fine, and StartWork () is called and everything is beautiful (the GUI works without blocks, etc.).
but thread-> quit () does nothing, it is called (because the button changes the text), but that's it. if I just call worker-> StopWork (), it works, but then I can not start it again.
I tried with thread-> exit (); but the results are the same. Also, I know that a subclass works, but it looks more ugly, and according to recent Qt documentation, the subclass is not optimal.
early.