[Qt-interest] Basic QThread question
Eirik Ulvik
eiriku at simsurgery.com
Thu May 6 11:12:15 CEST 2010
I had trouble using timers in threads before and used this info:
http://lists.trolltech.com/qt-interest/2007-09/thread00073-0.html.
Basically this would translate to:
>> class DevicePoller : public QObject
>> {
>> Q_OBJECT
>> public:
>> DevicePoller( QObject* parent = 0 )
>>
>> : QObject( parent ), m_timer( new QTimer( this ) )
>>
>> {
connect(this, SIGNAL(started()), this, SLOT(handleStarted()));
>> connect( m_timer, SIGNAL( timeout() ), SLOT( poll() ) );
>> m_timer->start( 1000 );
>> }
>>
>> public slots:
>> void poll()
>> {
>> // Talk to your device here
>> ...
>> int x = ...
>>
>> // Let the world know the value retrieved
>> emit value( x );
>> }
>>
private slots:
void handleStarted()
{
moveToThread(this);
}
>> signals:
>> void value( int x );
>>
>> private:
>> QTimer* m_timer;
>> };
>>
>>
>> class PollingThread : public QThread
>> {
>> Q_OBJECT
>> public:
>> PollingThread( QObject* parent = 0 )
>>
>> : QThread( parent )
>>
>> {}
>>
>> DevicePoller* poller() const { return m_poller; }
>>
>> protected:
>> virtual void run()
>> {
>> // Create an object to do the polling
>> m_poller = new DevicePoller;
>>
>> // Start the event loop
>> exec();
>>
>> // Clean up
>> delete m_poller;
>> m_poller = 0;
>> }
>>
>> private:
>> DevicePoller* m_poller;
>> };
>>
Regards,
Eirik
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100506/dbf17528/attachment.bin
More information about the Qt-interest-old
mailing list