QThreads freeze main aplpications while running
-
Hello,
I am trying to use QThreads now with the worker object approach.
In my main window class here is what I do
pThread = new controllerThread(pc); awgThread = new controllerThread(awgc);
where controllerThread is a simple extension of QThread class which overrides the run since I don't want the run to call the exec (don't want to execute a loop of events, just call a DOWork function in the threads that when is finished terminates the thread)
I am connecting then the two threads to the doWork function of pc and awgc through the following commands
connect(cThread,SIGNAL(started()),pc,SLOT(run_test())); connect(cThread,SIGNAL(started()),awgc,SLOT(run_test()));
where run test is the dowork function that I want to execute on the thread.
I am then connecting a pushbutton to the start of the thread where I am starting the two threads with the following lines
pc->moveToThread(pThread); pThread->start(); awgc->moveToThread(awgThread); awgThread->start();
the two threads are started correclty (I have a simple for loop that prints a counter), but the main window is completely freezed until the threads are terminated.
What am I missing.
Any help will be much appreciated.
Thank you!
-
OK seems I solved, as I was declaring pc and awgc as parent class of the main window (not sure how the threads were running at this point since it should not have been moving the threads to pThread and awgThread at all).
But my point is now, if I declare pc and awgc in mainwindow with no parent class, how is the destruction of these objects handled when the main application is closed?
THank you
-
OK seems I solved, as I was declaring pc and awgc as parent class of the main window (not sure how the threads were running at this point since it should not have been moving the threads to pThread and awgThread at all).
But my point is now, if I declare pc and awgc in mainwindow with no parent class, how is the destruction of these objects handled when the main application is closed?
THank you
@Gaetano03 said in QThreads freeze main aplpications while running:
how is the destruction of these objects handled when the main application is closed?
Then properly quit the thread in the mainwindow's dtor, wait for finish and connect the finished slot to deleteLater as explained in the documentation.