Próbuję uruchomić QTimer w określonym wątku. Jednak zegar nie wydaje się wykonywać i nic nie jest drukowane. Czy to ma coś wspólnego z zegarem, szczeliną lub wątkiem?Uruchamianie QTimer w QThread
main.cpp
#include "MyThread.h"
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
MyThread t;
t.start();
while(1);
}
MyThread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QTimer>
#include <QThread>
#include <iostream>
class MyThread : public QThread {
Q_OBJECT
public:
MyThread();
public slots:
void doIt();
protected:
void run();
};
#endif /* MYTHREAD_H */
MyThread.cpp
#include "MyThread.h"
using namespace std;
MyThread::MyThread() {
moveToThread(this);
}
void MyThread::run() {
QTimer* timer = new QTimer(this);
timer->setInterval(1);
timer->connect(timer, SIGNAL(timeout()), this, SLOT(doIt()));
timer->start();
}
void MyThread::doIt(){
cout << "it works";
}
To nie jest tak, jak powinno się już robić. [przeczytaj ten post] (http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/) – UmNyobe
dlaczego nie tworzysz QApplication? – Kunal
Link został zmieniony na: http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/ – donturner