how do you use threads in a very heavy loop process in one class and without using additional class objects?
-
I created a process in the loop and required me to use progresbar, but the problem is that progresbar and the window lag, people suggest using a thread object class with emit method but the problem is that my code is already quite a lot and it is not possible to move and rearrange everything it will make me crazy, is there any other way for this problem without having to create a new class to use threads?
-
@Blackzero said in how do you use threads in a very heavy loop process in one class and without using additional class objects?:
is there any other way for this problem without having to create a new class to use threads?
No.
Properly design your app 🙂 -
@Christian-Ehrlicher Is that the only way ?
-
@mpergand yes gradually, in the loop process or "for (;;)" calls one by one function or method after the first method is finished will run the second method in each method there is a loop back yes that is to display to progressesbar, but especially the loop that displays the progressesbar value so as not to lag I have tried using the
std::thread thread(&MainWindow::WorkerThread, this); thread.detach();
but an error appears
QObject::startTimer: Timers cannot be started from another thread -
@Blackzero said in how do you use threads in a very heavy loop process in one class and without using additional class objects?:
yes gradually,
So you need to have a instance var to memorize the current step.
For each step you need to call the event loop,
the idea in pseudo codevoid processNextStep() // must be defined as a slot
{
process current step
if last step return
current step++
update progressbar
call slot processNextStep // see below
}You have two ways to call the slot method
1- QMetaObject::invokeMethod in mode Qt::QueuedConnection
2- QTimer::singleShot((0)See the docs for more infos.