Hi together,

I have some problems with understanding the behaviour of my following code...
I tryed to udnerstand the Threading in Qt, so i found a nice youtube vid which i would like to share with you - its from Dario Freddi http://www.youtube.com/watch?v=MMFhc2jXzgw


I Wrote a simple application to try movetothread methode, just 2 Workers and a QWidget class with a button. on button clicked worker 1 starts -> in its cTor it creates worker2 and waits of the finish signal from worker2. But some code say more than words so.
i can post the whole simple project, but i dont know if its ok, so i just post the some few lines of code.

QWidget:
Qt Code:
  1. void Widget::on_btTest_clicked()
  2. {
  3. QTime time;
  4. time.start();
  5. worker1 *worker = new worker1;
  6. worker->doWork1();
  7. qDebug() << time.elapsed() << "mseconds" << "current time:" << time.currentTime().toString("hh:mm:ss:z")<< "worker1 result" << worker->getRes();
  8. }
To copy to clipboard, switch view to plain text mode 

Worker1:
Qt Code:
  1. worker1::worker1(QObject *parent) :
  2. QObject(parent),
  3. m_classThread(new QThread),
  4. m_resultString("Hallo")
  5. {
  6. moveToThread(m_classThread);
  7. m_worker2 = new Worker2;
  8. //breaks the own event loop if worker 2 is finished
  9. connect(m_worker2, SIGNAL(workDone()), &wait, SLOT(quit()));
  10. connect(this, SIGNAL(startNow()), m_worker2, SLOT(doWork2()));
  11. m_classThread->start();
  12. }
  13.  
  14. void worker1::doWork1()
  15. {
  16. //starts worker2 und wartet auf sein finished
  17. qDebug() << Q_FUNC_INFO;
  18. emit startNow();
  19. wait.exec();
  20. }
  21.  
  22. QString worker1::getRes()
  23. {
  24. return m_resultString;
  25. }
To copy to clipboard, switch view to plain text mode 

Worker2:
Qt Code:
  1. Worker2::Worker2(QObject *parent) :
  2. QObject(parent),
  3. m_classThread(new QThread),
  4. m_resultString("123")
  5. {
  6. moveToThread(m_classThread);
  7. tima.setInterval(5000);
  8. tima.start();
  9. m_classThread->start();
  10. }
  11.  
  12. void Worker2::doWork2()
  13. {
  14. qDebug() << Q_FUNC_INFO;
  15. // create a event loop for the timer to be finished
  16. QEventLoop wait;
  17. connect(&tima, SIGNAL(timeout()), &wait, SLOT(quit()));
  18. wait.exec();
  19. emit workDone();
  20.  
  21. }
To copy to clipboard, switch view to plain text mode 



Results in the Debug frame:
Qt Code:
  1. void worker1::doWork1()
  2. void Worker2::doWork2()
  3. void worker1::doWork1()
  4. void Worker2::doWork2()
  5. 5003 mseconds current time: "15:35:22:36" WIDGET: worker one "Hallo"
  6. 7204 mseconds current time: "15:35:22:36" WIDGET: worker one "Hallo"
To copy to clipboard, switch view to plain text mode 


So here is my missunderstood/question -> if i click the button twice with a simple delay of 2 seconds, why get i the results at the same time? Why i dont get the debug msg of the first click on the button from(worker.getRes() (in the widget class) not 2 seconds earlier than the msg of the 2nd click?

If I may suggest, then I would argue that the problem is that the queue for the widget/main thread queue is accessed when the 2nd click -> workers are finished? So the first debug msg got queued at the mainthread and fired when the last work is done? But i dont get it exactly whats the problem here.

It would be very nice if s.o can explain me the above behaviour. An it would be really nice , to see how to solve this "problem". thanks!
(if u want me to post the project as attachement - just tell me....)

TIA Anenja!