Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using QThread



  • 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.



  • Signals and slots come into mind...



  • 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.



  • [quote author="qwertyuiopearendil" date="1299610629"]I cannot use them[/quote]

    Oh, of course ... how could I miss that. My false, sorry.



  • [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...



  • 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.



  • Read something like: "this":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects to get some more ideas.



  • 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.



  • 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.



  • 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.



  • Take a look at the Mandelbrot example that ships with Qt. It uses the pattern you are after here I think.


Log in to reply