QThread is blocking UI-Thread
-
Hi,
I think a have a Little missunderstanding in using QThread. I have a WorkerThread that is doing some intense work and is sending a refresh-signal to the MainThread from time to time.
@
////////////////////// MAINTHREAD //////////////////////
void MainThread::StartWorkerThread()
{
QThread t;
bool retVal = connect(&t, SIGNAL(SigRefreshUI()), this, SLOT(Refresh()), Qt::DirectConnection);
bool retVal2 = connect(&t, SIGNAL(SigSendAsyncTCP(string)), this, SLOT(SendAsnyTCP(string)), Qt::DirectConnection);t.start();
}
void MainThread::GettingTCPAnswer(string answer)
{
emit SigSendAsyncTCP(answer);
}////////////////////// WORKERTHREAD //////////////////////
void WorkerThread::run()
{
bool retVal3 = connect((MainThread*)m_mainThread, SIGNAL(SigTCPAnswer(string)), this, SLOT(TCPAnswer(string)), Qt::DirectConnection);doSomeWork.... emit RefreshUI(); doSomeWork.... emit RefreshUI(); doSomeWork.... emit SigSendAsyncTCP("Somedata");
}
void WorkerThread::TCPAnswer(string answer)
{
doSomeWork....emit RefreshUI(); doSomeWork.... emit RefreshUI();
}
@Everthing is fine so far. In the run method the UI is refreshed instantly after the signal "RefreshUI" is emited. After sending the TCPData through the MainThread and getting the answer the slot "TCPAnswer" is called. This time the WorkerThread is blocking the MainThread. The UI is refreshed after leaving the scope of "TCPAnswer" and not everytime "RefrehUI" is emited.
Could someone explain me what I´m doing wrong?
Thanks and regards
-
Hi,
This:@Thread t@ doesn't really start a new thread! It only creates an object of the type thread. You need to move that object to an other thread by moveToThread();
It would be advisable to keep the thread pointer into your member variables to not loos control off it.
In your debugger you should be able to see the added thread if you are successful in it.