I was intrigued by the post http://labs.qt.nokia.com/2010/06/17/...oing-it-wrong/ and really like that idea of new way of using threads. So I wrote a simple example, which... does not work
Qt Code:
  1. #ifndef THREADs_TEST_H
  2. #define THREADs_TEST_H
  3.  
  4. #if defined(_WIN32)
  5. #include <windows.h>
  6. #elif defined(__GNUC__)
  7. #include <unistd.h>
  8. #define Sleep(x) sleep((x)/1000)
  9. #endif //_WIN32
  10.  
  11. #include <iostream>
  12. #include <QThread>
  13. #include <QObject>
  14. #include <QDialog>
  15. #include <QDialogButtonBox>
  16. #include <QBoxLayout>
  17. #include <QPushButton>
  18.  
  19. class Cycler : public QObject
  20. {
  21. Q_OBJECT
  22. public:
  23. Cycler() : QObject() {};
  24. virtual ~Cycler(){stop();};
  25. signals:
  26. void done();
  27. public slots:
  28. void cycle() {
  29. arbeit = true;
  30. int i = 0;
  31. while(arbeit) {
  32. Sleep(1000);
  33. std::cout << i++ << " " << std::flush;
  34. };
  35. emit done(); };
  36. void stop(){ arbeit = false;};
  37. private:
  38. volatile bool arbeit;
  39. };
  40.  
  41. class CyclerDialog: public QDialog
  42. {
  43. Q_OBJECT
  44. public:
  45. explicit CyclerDialog(QWidget* parent = 0, Qt::WindowFlags f = 0) : QDialog(parent, f){
  46. QVBoxLayout *main_layout = new QVBoxLayout;
  47. main_layout->addWidget(buttons);
  48. setLayout(main_layout);
  49.  
  50. cycl.moveToThread(&thrd);
  51. connect(buttons, SIGNAL(accepted()), &thrd, SLOT(start()));
  52. connect(&thrd, SIGNAL(started()), &cycl, SLOT(cycle()));
  53. connect(buttons, SIGNAL(rejected()), &cycl, SLOT(stop()));
  54. connect(&cycl, SIGNAL(done()), &thrd, SLOT(quit())); };
  55.  
  56. virtual ~CyclerDialog(){thrd.wait(1000);};
  57. private:
  58. Cycler cycl;
  59. QThread thrd;
  60. };
  61.  
  62. #endif // THREADs_TEST_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include <QApplication>
  2. #include "threads_test.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication app(argc, argv);
  7. CyclerDialog window;
  8. window.show();
  9. return(app.exec());
  10. }
To copy to clipboard, switch view to plain text mode 

It is supposed to start printing numbers in std::cout when OK is clicked, and stop when CANCEL is clicked, but apparently stop() slot is never called. WHY????