Solved QThread freezes whole app.
-
Hi! You used the wrong and old way of creating threads in
Qt
, that's why you must use 1 method in the run and create another worker for another method. I suggest you to use it right: How To Really, Truly Use QThreads; The Full Explanation. Using the new method of creating threads, you can create as many threads as you want from the single worker class.Also, you can use threads from standard C++:
#include <thread> std::thread firs(&MWindow::firstFunction); std::thread sec(&MWindow::secondFunction);
-
Also, you can use threads from standard C++:
#include <thread> std::thread firs(&MWindow::firstFunction); std::thread sec(&MWindow::secondFunction);
Well, i tried, and there is like 15 errors appear:
-
That said, fire and forget long functions than run a long time can be done (threaded) via QtConcurrent, thats in your case here, also pretty much a one liner.
void MWindow::on_doButton_clicked() { QtConcurrent::run(firstFunction); QtConcurrent::run(secondFunction); }
Well)) for that case, i got 199 errors when i added code as you suggested:
#include <QtConcurrent/QtConcurrent> void MWindow::on_doButton_clicked() { QtConcurrent::run(doProcess); QtConcurrent::run(doProcess2); }
-
I suggest you to use it right: How To Really, Truly Use QThreads; The Full Explanation. Using the new method of creating threads, you can create as many threads as you want from the single worker class.
Already tried that yesterday night. Everything seems perfect, no errors, but at the run time i got 4 alarms in Application Output about 3 signals in my Worker class and 1 slot. I didnt figure out whats wrong, but now i'm googling such issue
"QObject::connect: No such signal"
P.S. yes, my slot function in worker class in public slots: and signals: is just signal, they cant be in public.
-
@Engelard
Hi you have included QObject in worker class ?
class Worker : public QObject {
Q_OBJECT <<<<< -
@mrjj ah, forgot about that macro(never notice that, it always awtomaticly was created by QT)
Now i add it, and it adds me even more errors, but here is whole my code of worker header file:
#pragma once #include <QtCore> #include <QObject> #include <Windows.h> class TestThreads : public QObject { Q_OBJECT public: TestThreads(); static unsigned int addressNow; unsigned int addressMAX = 10; int myValue; bool scanDone = false; DWORD objID; public slots: void scanProcess(); signals: void addressFound(unsigned int addr); void addressValue(int val); void scanFinished(); };
And the result of adding that Q_OBJECT macro is this:
-
@Engelard
Well that is a normal case
Clean your build folder ( just delete all files)
and then run qmake.
Then rebuild all and it should be happy. -
@mrjj now there is new 3 errors)
-
@Engelard
Hi
Its "already defined" so check your .pro file
if you have the file(s) included twice for some reason. -
@mrjj nope, everything fine in there, all i have there is mentioned only once
-
@Engelard
hmm.
what files does class TestThread live it ?
Also did you give your signals a body in cpp for
void addressFound(unsigned int addr);
void addressValue(int val);
void scanFinished();
as it seems to complain about those.
(signals should not have a body) -
(signals should not have a body)
OMG, i didnt know, and as habbit i ofcource gave body(definitions) for every signal function.
Now its working, and threads working perfectly, thank you!
-
@Engelard
Yep its a true classic as normally u must always give a body or it complains about that.
but signals are just the signatures. -
@Engelard you need to properly tag people😉
Anyway QtConcurrent is its own modul, you‘ll need to add it in your pro file as such, as it is stated in the docuQT += concurrent
than it should work, now that you have removed the function body of the signal ☺️