Hello everyone,

I'm not really used to using Qt, but for my work, I'v to do with it . I 've tried to make a generic thread Worker for my actions in multithreaded application. Here is my code

ThreadWorker.h
Qt Code:
  1. #ifndef _THREADWORKER_H_
  2. #define _THREADWORKER_H_
  3.  
  4. #include "QtCore/qobject.h"
  5. #include "QtCore/qthread.h"
  6. #include "QtCore/qtimer.h"
  7.  
  8. class ThreadWorker : public QObject
  9. {
  10. Q_OBJECT
  11.  
  12. private:
  13. int _frequency;
  14. QTimer _timer;
  15.  
  16. public:
  17. ThreadWorker(int frequency = 300);
  18. ~ThreadWorker();
  19.  
  20. /*!
  21. \brief Create a worker which call the specified slot in th specified thread
  22. \param thread The thread to use
  23. \param source The object which contains the slot to use
  24. \param slot The slot to use
  25. */
  26. void createWorker(QThread *thread, const QObject* source, const char *slot);
  27.  
  28. /*!
  29. \brief Sets the call frequency for the threaded method
  30. \param frequency Frequency in millsecond (default value: 300ms)
  31. */
  32. void setFrequency(int frequency);
  33.  
  34.  
  35. private slots:
  36. void start();
  37. void stop();
  38.  
  39. public slots:
  40. /*!
  41. \brief a slot to indicates the end of our current work
  42. */
  43. void terminate();
  44.  
  45. signals:
  46. void finished();
  47. };
  48.  
  49. #endif
To copy to clipboard, switch view to plain text mode 

ThreadWorker.cpp
Qt Code:
  1. #include "ThreadWorker.h"
  2.  
  3. ThreadWorker::ThreadWorker(int frequency): _frequency(frequency), _timer(this)
  4. {}
  5.  
  6. ThreadWorker::~ThreadWorker()
  7. {
  8. terminate();
  9. }
  10.  
  11. void ThreadWorker::createWorker(QThread *thread, const QObject* source, const char *slot)
  12. {
  13. connect(&_timer, SIGNAL(timeout()), source, slot, Qt::DirectConnection);
  14.  
  15. connect(thread, SIGNAL(started()), this, SLOT(start()), Qt::DirectConnection);
  16. connect(this, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
  17. connect(this, SIGNAL(finished()), this, SLOT(stop()), Qt::DirectConnection);
  18.  
  19. moveToThread(thread);
  20. }
  21.  
  22. void ThreadWorker::setFrequency(int frequency)
  23. {
  24. _frequency = frequency;
  25. }
  26.  
  27. void ThreadWorker::start()
  28. {
  29. _timer.setInterval(_frequency);
  30. _timer.start();
  31. }
  32.  
  33. void ThreadWorker::stop()
  34. {
  35. if(_timer.isActive())
  36. {
  37. _timer.stop();
  38. }
  39. }
  40.  
  41. void ThreadWorker::terminate()
  42. {
  43. if(_timer.isActive())
  44. {
  45. emit finished();
  46. }
  47. }
To copy to clipboard, switch view to plain text mode 

And here is the code which use the ThreadWorker
Qt Code:
  1. #include "ThreadWorker.h"
  2.  
  3. class MyObject : public QObject
  4. {
  5.  
  6. Q_OBJECT
  7.  
  8. private
  9. QThread _thread;
  10. ThreadWorker _worker;
  11.  
  12. public:
  13. MyObject();
  14. ~MyObject();
  15. void start();
  16. void stop();
  17.  
  18. public slots:
  19. void process();
  20. };
  21.  
  22. MyObject::MyObject(): _thread(this)
  23. {}
  24.  
  25. MyObject::~MyObject()
  26. {
  27. if(_thread.isRunning())
  28. {
  29. _worker.terminate();
  30. _thread.wait(500);
  31. }
  32. }
  33.  
  34. void MyObject::start()
  35. {
  36. if(_thread.isRunning())
  37. {
  38. return;
  39. }
  40.  
  41. _worker.createWorker(&_thread, this, SLOT(process()));
  42.  
  43. _thread.start();
  44. }
  45.  
  46. void MyObject::process()
  47. {
  48. //do something
  49. }
  50.  
  51. void MyObject::stop()
  52. {
  53. _worker.terminate();
  54. }
To copy to clipboard, switch view to plain text mode 

I run this code in unit tests with QTest library and I use the macro QTEST_MAIN.
The execution seems fine until the following error:
Program: …
Module: 4.7.4
File: global\qglobal.cpp
Line:

ASSERT failure in QCoreApplication::sendEvent: “Cannot send events to objects owned by a different thread. Current thread 3f6a90.
Receiver ‘’ (of type ‘ThreadWorker’) was created in thread 12ef5c”, file kernel\qcoreapplication.cpp, line...
I do not really see where it comes from :/. Only thing is that this does not occur if I do not put this line moveToThread (thread), however, in this case, the slot process() is never called.

Does anyone have any idea what I am doing wrong?

Thank you in advance for any help you can provide me

ps: Sorry if my english is not correct :/