[Qt-interest] Basic QThread question

Phil phillor at telstra.com
Thu May 13 12:17:51 CEST 2010


On Fri, 7 May 2010 06:14:02 pm Sean Harmer wrote:

> Please try the attached application. Instead of querying your device it
> simply generates a random number. If you set breakpoints in the
> DataProcessor::processData(int x) and DevicePoller::poll() methods then
> look at the Threads debug view in creator you will see that this does
> work using two threads and that the "polling" is performed in the worker
> thread as expected. The main thread function simpy prints out the square
> of the random number generated by the worker thread.
> 
> Hopefully this will allow you to get it working with your polling code.
> Just replace the call to qrand() % 10 with your own code.

I'm almost afraid to have to ask for some more hand-holding.

The code that Sean kindly put together for me does work correctly, 
however, there is a small complication. I'll try to explain this without too 
much code.

The main class, derived from QMainWindow has a drop down selection box. 
The item selected determines the variables sent to several functions. The 
hardware is then polled and the returned data forms part of further 
calculations.

I've pasted Sean's code into my selectItme function as follows:

void Pest::selectItem(const QString &listItem)
{
    DevicePoller poller;    //I think there may be a problem here
    poller.stop();

    //pass variables based on the selection to a calculation class

    //restart the thread timer 

    //Sean's code follows		

    // Get the worker thread to start but wait until the
    // DevicePoller thread has actually been created.
    m_workerThread->waitForStart();

    // It is now safe to connect to the DevicePoller's signals
    connect( m_workerThread->poller(), SIGNAL( value( int ) ),
             this, SLOT( processData( int ) ) );

    // Note that starting the worker thread does not automatically
    // start the timer that controls the polling. We now do this
    // explicitly so that we do not miss any data that could have
    // been collected between the calls to waitForStart() and connect.
    // We can either call the DevicePoller::start() slot using a
    // singleshot timer or invokeMethod.
    QTimer::singleShot( 0, m_workerThread->poller(), SLOT( start() ) );
}

This works perfectly the first time, however, a subsequent selection fails. It 
seems that the timer thread does not restart.

No doubt the answer is obvious, but not to me. Can anyone help, please?

-- 
Regards,
Phil



More information about the Qt-interest-old mailing list