Qt MainWindow is not updating in multithreaded application
-
thank you for your help guys!
I changed the class to a worker object with will be moved to the thread.now i have another problem:
I have i QTimer which should come around 500ms (is not a big deal if it is a little bit later).
But with the new implementation i get no event from the timer anymore. I think the problem is, what Andre was writing, that the new thread has no event loop.some code:
@NFC_Thread::NFC_Thread(NFC_Reader * Reader)
{
mReader = Reader;
mError = 0;
mStopPolling = false;if(mpTimer != 0) { delete mpTimer; } mpTimer = new QTimer; QObject::connect(mpTimer, SIGNAL(timeout()), this, SLOT(ResetTargets())); mpTimer->start(mTimerMS);
}
void NFC_Thread::doPolling()
{
mError = 0;
mStopPolling = false;if(mpTimer != 0) { delete mpTimer; } mpTimer = new QTimer; QObject::connect(mpTimer, SIGNAL(timeout()), this, SLOT(ResetTargets())); mpTimer->start(mTimerMS);
while(!mStopPolling)
{
nfc_target Target;
mError = nfc_initiator_poll_target(mReader->GetDevice(), nmModulations, szModulations, mPollNr, mPollPeriod, &Target);
if(mError > 0)
{
cout << "NFC: found target" << endl;
}
#warning Bug in driver: Timeout generate a NFC_EIO Error, 'https://code.google.com/p/libnfc/issues/detail?id=224'
else if(mError > 0)
{
cout << "NFC: Error" << endl;
mStopPolling = true;
}
else
{
cout << "NFC: no target found" << endl;
}
}
}void NFC_Thread::ResetTargets()
{
for(QList<nfc_target>::iterator iter = mTargets.begin(); iter != mTargets.end(); iter++)
{
emit TargetRemoved(*iter);
}
mTargets.clear();
mpTimer->start(mTimerMS);
Debug("NFC: Reseting List with timeout!");
}@I create the Thread object in a other class with this:
@
mpThread = new NFC_Thread(this);
mpRunningThread = new QThread;
mpThread->moveToThread(mpRunningThread);connect(mpRunningThread, SIGNAL(started()), mpThread, SLOT(doPolling())); connect(mpThread, SIGNAL(finished()), mpRunningThread, SLOT(quit())); connect(mpThread, SIGNAL(finished()), mpThread, SLOT(deleteLater())); connect(mpRunningThread, SIGNAL(finished()), mpRunningThread, SLOT(deleteLater()));
@
and start the thread with this:
@mpRunningThread->start();@now the question is, where should i put the event loop...
the main problem is: i want to write some classes around a lib, to get support of signals and slots. -
By default a QThread already has its own eventloop. The default implementation of run() already calls exec(). Your problem is the busy loop :
@
while(!mStopPolling)
{
nfc_target Target;
.
.
.
}
@
while this is running your code does not return to the eventloop. So no signals are handled. Infinite loops or polling in loops is not a good idea in an event driven framework like Qt. -
Ok! But i have this libnfc, and i can not change it, so i want to write a wrapper around it (from poll to event driven).
what is the best option for me to do this? -
don't loop forever, use a timer to poll every n ms instead. Returning quickly when there is nothing.
-
How long do you estimate does this call need to return ?
@
mError = nfc_initiator_poll_target(mReader->GetDevice(), nmModulations, szModulations, mPollNr, mPollPeriod, &Target);
@ -
Sometimes a tight loop is a good solution, but then you just have to make sure that you don't use any slots. If you're in a loop, and you need a timer-like solution, you could also just check a [[doc:QElapsedTimer]] in every iteration.
-
The poll needs currently 5 sek to return (still a bug in the lib), so it is like really long. I think the best is, to wait until the bug is fixed, making the poll short as possible and i can change the howl algorithm to something better. the timer is just there because the poll needs so long. Example: If i am returning a tag from the reader and putting the tag again in the 5 sek, the lib does not recognizing that there is a "new" tag.
-
KA510; I don't know how long it takes to return from nfc_initiator_poll_target(), there is no way to know without measuring since I don't know that call.
I suggest using a timer and start it just before the call, and print 'elapsed()' directly after the call completed.
Make sure you do it in different situations and repeatedly to get a good idea how long it takes on average.
-
[quote author="Thomas Zander" date="1360915643"]KA510; I don't know how long it takes to return from nfc_initiator_poll_target(), there is no way to know without measuring since I don't know that call.
I suggest using a timer and start it just before the call, and print 'elapsed()' directly after the call completed.
Make sure you do it in different situations and repeatedly to get a good idea how long it takes on average.[/quote]
I wasn't asking you but the original poster of course. Why would you think that question was directed to you?
On topic: If the call takes 5 seconds you can not expect a timer with a 500 ms timeout to work as expected ...
Instead of the loop just call the slot doProlling() at start of the thread (as you do now, but without the loop). When the nfc call returns emit a signal connected to a slot in the worker object, which then checks all your abort flags and such (you could also update your targets here) and then call the doPolling slot again if the abort flag is not true. This way your thread is still blocked while executing the nfc blocking call but atleast returns to the event loop right after that. -
this sounds good to me, i will give change it to this structure.
I will also change to another lib for the nfc part because the reader is not very well supported.