Starten von QTimer in einem QThread

Ich versuche, einen QTimer in einem bestimmten Thread zu starten. Der Timer scheint jedoch nicht zu funktionieren und es wird nichts ausgedruckt. Hat das etwas mit dem Timer, dem Slot oder dem Thread zu tun?

main.cpp

<code>    #include "MyThread.h"
    #include <iostream>
    using namespace std;

    int main(int argc, char *argv[]) {
        MyThread t;
        t.start();
        while(1);
    }
</code>

MyThread.h

<code>    #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 */
</code>

MyThread.cpp

<code>    #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";
    }
</code>

Antworten auf die Frage(6)

Ihre Antwort auf die Frage