Hi everyone,
I have one GUI class, one thread class, and one timer inside thread class.

mainwindow.h file:

Qt Code:
  1. class testClass : public QThread
  2. {
  3. Q_OBJECT
  4. public:
  5. testClass();
  6. void run();
  7. public slots:
  8. void timerExpired();
  9. void startWork();
  10.  
  11. private:
  12. QTimer *timer;
  13. };
  14.  
  15. class MainWindow : public QMainWindow
  16. {
  17. Q_OBJECT
  18.  
  19. public:
  20. explicit MainWindow(QWidget *parent = 0);
  21. ~MainWindow();
  22.  
  23. signals:
  24. void startSignal();
  25.  
  26. private slots:
  27. void on_pushButton_clicked();
  28.  
  29. private:
  30. Ui::MainWindow *ui;
  31. };
To copy to clipboard, switch view to plain text mode 

mainwindow.c

Qt Code:
  1. void MainWindow::on_pushButton_clicked()
  2. {
  3. emit startSignal();
  4. }
  5.  
  6.  
  7. testClass::testClass()
  8. {}
  9.  
  10. void testClass::run()
  11. {
  12. timer = new QTimer();
  13. QObject::connect(timer,SIGNAL(timeout()),this,SLOT(timerExpired()),Qt::DirectConnection);
  14. exec();
  15. }
  16.  
  17. void testClass::startWork()
  18. {
  19. timer->start(0);
  20. }
  21.  
  22. void testClass::timerExpired()
  23. {
  24. qDebug("Timer expired.");
  25. }
To copy to clipboard, switch view to plain text mode 

main.cpp

Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QApplication a(argc, argv);
  4. MainWindow w;
  5. testClass tCls;
  6. QObject::connect(&w,SIGNAL(startSignal()),&tCls,SLOT(startWork()),Qt::QueuedConnection);
  7. w.show();
  8. tCls.start(QThread::NormalPriority);
  9.  
  10. return a.exec();
  11. }
To copy to clipboard, switch view to plain text mode 

In this case I am getting a run time error "QObject::startTimer: timers cannot be started from another thread".

What I understood :

timer is allocated inside run() so it belogs to testClass thread.But when i was trying to call the testClass::startWork() slot from mainwindow at that time testClass::startWork() belongs to mainwindow thread. So the timer is not able to start.
If I will take timer as static(from stack) then it will be fine because timer will be in main thread.

What i have mentioned here is it true ?
Is the member function of testClass belongs to testClass thread or not ?
I am using signal slot to communicate between two thread .But the current thread id of mainwindow and startWork() slot are same.
How i solve this ?

thanks.