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>
{
Q_OBJECT
public:
~MyThread(){ delete timer;}
void run(void)
{
qDebug() << "head of run";
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:
};
#endif //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
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QtGui>
#include "mythread.h"
int main(int argc, char* argv[])
{
MyThread thread;
thread.start();
return app.exec();
}
#include <QtGui>
#include "mythread.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyThread thread;
thread.start();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
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...
Have a nice evening
Col
Bookmarks