ColonelMoW
30th May 2008, 21:42
Hi!
Due to the well known bug with TableView in combination with sorting I had to switch from
Qt 4.3 to Qt 4.4. Everything went pretty well until it tried my threading-classes. Here I have a strange issue which I haven't had with Qt 4.3. I wrote a small example:
mythread.h:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTimer>
#include <QDebug>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QObject *parent = 0) : QThread(parent){}
~MyThread(){ delete timer;}
void run(void)
{
qDebug() << "head of run";
timer = new QTimer;
timer->setSingleShot(true);
timer->setInterval(1000);
connect( timer, SIGNAL(timeout()), this, SLOT(restartTimer()));
timer->start();
qDebug() << "about to start event loop";
exec();
}
private slots:
void restartTimer(void)
{
timer->start();
qDebug() << "Timer restarted";
}
private:
QTimer *timer;
};
#endif //MYTHREAD_H
main.cpp:
#include <QtGui>
#include "mythread.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyThread thread;
thread.start();
return app.exec();
}
When I compile it with Qt 4.4.0 I get the following output:
head of run
about to start event loop
QObject::startTimer: timers cannot be started from another thread
Timer restarted
And then nothing. Which is logical because it can't access the timer. But why?
Maybe someone could try this code on his machine. It would be really great!
Or maybe someone could tell me why this doesn't work...:confused:
Have a nice evening
Col
Due to the well known bug with TableView in combination with sorting I had to switch from
Qt 4.3 to Qt 4.4. Everything went pretty well until it tried my threading-classes. Here I have a strange issue which I haven't had with Qt 4.3. I wrote a small example:
mythread.h:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTimer>
#include <QDebug>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QObject *parent = 0) : QThread(parent){}
~MyThread(){ delete timer;}
void run(void)
{
qDebug() << "head of run";
timer = new QTimer;
timer->setSingleShot(true);
timer->setInterval(1000);
connect( timer, SIGNAL(timeout()), this, SLOT(restartTimer()));
timer->start();
qDebug() << "about to start event loop";
exec();
}
private slots:
void restartTimer(void)
{
timer->start();
qDebug() << "Timer restarted";
}
private:
QTimer *timer;
};
#endif //MYTHREAD_H
main.cpp:
#include <QtGui>
#include "mythread.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyThread thread;
thread.start();
return app.exec();
}
When I compile it with Qt 4.4.0 I get the following output:
head of run
about to start event loop
QObject::startTimer: timers cannot be started from another thread
Timer restarted
And then nothing. Which is logical because it can't access the timer. But why?
Maybe someone could try this code on his machine. It would be really great!
Or maybe someone could tell me why this doesn't work...:confused:
Have a nice evening
Col