Boron
30th March 2009, 17:41
Hi there,
I am playing around with QThreads for the first time. And now I have the weird problem that the QTimer in my thread class doesn't start or emit the timeout() signal. Some code:
CanThread::QCanThread( QObject* parent )
: QThread( parent )
{
m_countTimer = new QTimer( this );
m_countTimer->setInterval( 1000 );
connect( m_countTimer, SIGNAL(timeout()), this, SLOT(updateCounter()) );
}
void CanThread::run() {
m_countTimer->start();
exec();
}
void CanThread::stop() {
m_countTimer->stop();
this->quit();
}
void QCanThread::updateCounter() {
m_currentCounter++;
emit showCurrentCounter( m_currentCounter );
} The timer m_countTimer shall timeout all 1000ms. As reaction a counter variable is incremented and the GUI is signalled to display that variable.
(I know it is bullshit to do this that way. A simple timer and the GUI without my CanThread would be enough, but I want get a feeling how deal with threads)
But the slot updateCounter() is never called. Either the timer doesn't start when I call threadObject->start() or the timer doesn't emit its signal timeout().
What is wrong with my code?
I am playing around with QThreads for the first time. And now I have the weird problem that the QTimer in my thread class doesn't start or emit the timeout() signal. Some code:
CanThread::QCanThread( QObject* parent )
: QThread( parent )
{
m_countTimer = new QTimer( this );
m_countTimer->setInterval( 1000 );
connect( m_countTimer, SIGNAL(timeout()), this, SLOT(updateCounter()) );
}
void CanThread::run() {
m_countTimer->start();
exec();
}
void CanThread::stop() {
m_countTimer->stop();
this->quit();
}
void QCanThread::updateCounter() {
m_currentCounter++;
emit showCurrentCounter( m_currentCounter );
} The timer m_countTimer shall timeout all 1000ms. As reaction a counter variable is incremented and the GUI is signalled to display that variable.
(I know it is bullshit to do this that way. A simple timer and the GUI without my CanThread would be enough, but I want get a feeling how deal with threads)
But the slot updateCounter() is never called. Either the timer doesn't start when I call threadObject->start() or the timer doesn't emit its signal timeout().
What is wrong with my code?