Using QThread
-
wrote on 8 Mar 2011, 18:31 last edited by
Hi All,
I'm using an application that uses QThread, only one, it provides the new chart that i should show into my QGraphicsObject Class.
My Question is:
If i should request another chart when my thread is running, what can i do to send a new chart request to my thread even if it already running to provide the old one?
Thanks. -
wrote on 8 Mar 2011, 18:35 last edited by
Signals and slots come into mind...
-
wrote on 8 Mar 2011, 18:57 last edited by
I cannot use them, in my my mind the best thing should be for example call start when i must recalc the chart, but i've read into documentation that if i call start when thread is still running it doesn nothing.
-
wrote on 8 Mar 2011, 19:07 last edited by
[quote author="qwertyuiopearendil" date="1299610629"]I cannot use them[/quote]
Oh, of course ... how could I miss that. My false, sorry.
-
wrote on 8 Mar 2011, 19:18 last edited by
[quote author="Volker" date="1299611262"][quote author="qwertyuiopearendil" date="1299610629"]I cannot use them[/quote]
Oh, of course ... how could I miss that. My false, sorry.[/quote]
Aaah! !http://circleofcrones.co.uk/images/smilies/sarcasm.gif!qwertyuiopearendil, you are probably shooting yourself in the foot by not using signal-slot connections here...
-
wrote on 8 Mar 2011, 19:26 last edited by
I'm using thread to handle when a valid chart is ready, but can happen that I should invalidate the last one calc to recalc, and this could happen when a thread is running.
Thanks. -
wrote on 8 Mar 2011, 19:29 last edited by
Read something like: "this":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects to get some more ideas.
-
wrote on 9 Mar 2011, 09:14 last edited by
I guess it depends on the code in your thread that generates your chart for you. I am assuming, based on your hints above, that this is probably a single procedure of some kind that does not return to the event loop of the thread, making it impossible for (queued) signals to be delivered in to your thread in the meantime.
So, you need to create some way your procedure can be interrupted. One way would be to check if a cancel flag has been set at regular intervals. You can set such a flag using a direct method call (you can use a signal/slot connection, but not a queued connection as will be the default between threads). If a request for a new chart comes in while you are already processing a current chart, what I would do is to queue the request, set a cancel flag for the current request, and have your thread check after it is cancelled if there is a new request in the queue, and if so, process it.
-
wrote on 9 Mar 2011, 10:51 last edited by
Or call one of the "QCoreApplication::processEvents() ":http://doc.qt.nokia.com/4.7/qcoreapplication.html#processEvents methods regularly (seems you are in a kind of loop anyways) and have the events delivered and the slots called.
-
wrote on 9 Mar 2011, 18:03 last edited by
You can try this:
@
void MyThread::restartMe()
{
m_stopMe = true;
while(this->isRunning())
{
//wait and do nothing
}
this->start();
}void MyThread::run()
{
...
if(m_stopMe)
return;
...
if(m_stopMe)
return;
...
if(m_stopMe)
return;
...
}@
So you can call restartMe from outside of the QThread to restart the thread. -
wrote on 9 Mar 2011, 19:31 last edited by
Take a look at the Mandelbrot example that ships with Qt. It uses the pattern you are after here I think.
1/11