P
P
Pavel K2016-02-04 15:45:26
Qt
Pavel K, 2016-02-04 15:45:26

Endless loop in Qthead, how to do it right?

Greetings!
It is required to make an infinite loop to process some data,
the loop is done, it is put into a separate class in the thread, everything works, everything is ok.
But it was necessary to process the signals of the slot, namely, to slow down the cycle, as the slot worked, to continue it. To do this, I already created a bunch of variables for certain slots for each action. In my opinion, the code began to smell of something ...
Maybe there is a "correct" way of doing things in such situations?
As I see it ideally:
in the class, the same function is called instead of the eternal loop, as soon as an event (signal) arrives in the class, it suspends the call and calls the desired slot, as it worked, continues.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Obukhov, 2016-02-22
@PavelK

For a constant loop, you'd be better off implementing a slot that will be constantly called at the end of itself:
test.h:

private slots:
   void workMethod();

test.cpp:
void Class:workMethod()
{
     ....
    QMetaObject::invokeMethod(this, "workMethod", Qt::QueuedConnection);
}

Then your method will execute each iteration under the control of the message queue and at any time you can stop the thread.

P
Peter, 2016-02-04
@petermzg

And how do you implement a constant call of the same function without an eternal loop?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question