Hi all,

I'm new to threading in Qt.
I have several questions regarding the following class. I hope that wise programmers of this forum will help me better understand the QThread class and how does threads works in Qt.

thread1.h
Qt Code:
  1. #ifndef THREAD1_H
  2. #define THREAD1_H
  3.  
  4. #include <QThread>
  5. #include <QTimer>
  6.  
  7. class Thread1 : public QThread
  8. {
  9. Q_OBJECT
  10. public:
  11. Thread1();
  12. ~Thread1();
  13.  
  14. protected:
  15. void run();
  16.  
  17. signals:
  18. void sigThreadQuit();
  19.  
  20. public slots:
  21. void sTimeout();
  22.  
  23. private:
  24. QTimer* timer1;
  25. QTimer timer2;
  26.  
  27. int counter;
  28. };
  29.  
  30. #endif // THREAD1_H
To copy to clipboard, switch view to plain text mode 

thread1.cpp
Qt Code:
  1. #include "thread1.h"
  2.  
  3. Thread1::Thread1():
  4. timer1(new QTimer(this)), counter(0)
  5. {
  6. qDebug("Thread1(): Creating...");
  7. timer1->setInterval(1000);
  8. timer2.setInterval(1000);
  9.  
  10. connect(timer1, SIGNAL(timeout()), this, SLOT(sTimeout()));
  11. connect(&timer2, SIGNAL(timeout()), this, SLOT(sTimeout()));
  12.  
  13. moveToThread(this);
  14. }
  15.  
  16. Thread1::~Thread1() {
  17. qDebug("~Thread1(): Destroyed.");
  18. }
  19.  
  20. void Thread1::run() {
  21. qDebug("Thread1::run(): Running...");
  22. timer1->start();
  23. timer2.start();
  24. exec();
  25. }
  26.  
  27. void Thread1::sTimeout() {
  28. QTimer* t = (QTimer*) sender();
  29. qDebug("sTimeout(): Timer: %p; Counter: %d", t, counter);
  30. if(counter > 10)
  31. emit sigThreadQuit();
  32. counter++;
  33. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include <QtCore/QCoreApplication>
  2.  
  3. #include "thread1.h"
  4. #include "thread2.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QCoreApplication a(argc, argv);
  9. qDebug("Main start");
  10.  
  11. // Thread1* t1 = new Thread1();
  12.  
  13. Thread1 t2;
  14.  
  15. // Thread2 t2;
  16.  
  17. // a.connect(t1, SIGNAL(sigThreadQuit()), &a, SLOT(quit()));
  18. a.connect(&t2, SIGNAL(sigThreadQuit()), &a, SLOT(quit()));
  19.  
  20. // t1->start();
  21. t2.start();
  22.  
  23. int retvel = a.exec();
  24.  
  25. qDebug("Main end");
  26. return retvel;
  27. }
To copy to clipboard, switch view to plain text mode 

The main function creates an object of class Thread1, which is designed to display text and after 10 seconds to send a signal to finish the thread.
After creating a t2 object, signal sending by a thread is connected with the slot quit() of the main application.
In the above class, I have two objects QTimer - one of them is a pointer.
After running the application I see that only one timer is working - a pointer.

Here are my questions:
1. Why I can not see the operation of the QTimer object which is not a pointer? Does is not running?
2. If I don't use function moveToThread() for instance in the constructor of Thread1 class, then QTimer objects (or a thread), do not start in general. I think that it has something with main a.exec() function. Why and what?
3. Does the above example is written correctly with the principles of multithreaded programming in Qt?

I wrote this class for understanding Qt thread support. Any posts with good explanations and links are welcome.

Thank you for any answers