Results 1 to 7 of 7

Thread: QThread and QTimer

  1. #1
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QThread and QTimer

    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.

  2. #2
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QThread and QTimer

    why do you need a timer inside a thead!!! you can trigger the segment you are willing to trigger with the thread itself.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread and QTimer

    Quote Originally Posted by bibhukalyana View Post
    What i have mentioned here is it true ?
    Yes.

    Is the member function of testClass belongs to testClass thread or not ?
    Functions do not belong to threads. Only objects do in the sense that a thread processes events of an object.

    I am using signal slot to communicate between two thread .But the current thread id of mainwindow and startWork() slot are same.
    That's because the QThread instance lives in the main thread so its slots are executed in the context of the main thread.

    How i solve this?
    Instead of subclassing QThread, create an instance of it, wrap the functionality you want into a QObject instance and move that object into the thread.


    Qt Code:
    1. t.start();
    2. Object *o = new Object;
    3. o->moveToThread(&t);
    4. connect(mainWin, SIGNAL(someSignal()), o, SLOT(doSomeWork()));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QTimer

    Thanks for reply.

    I tried like :

    Qt Code:
    1. testClass *tCls = new testClass();
    2.  
    3. t.start();
    4. tCls->moveToThread(&t);
    5.  
    6. QObject::connect(&w,SIGNAL(startSignal()),tCls,SLOT(startWork()),Qt::QueuedConnection);
    7. w.show();
    To copy to clipboard, switch view to plain text mode 

    I am getting segmentation fault.
    Or will I take one more class and inherits QThread and after that execute movetoThread.

    One more thing. It is working but I dont know this is corret or not .

    Qt Code:
    1. tCls->moveToThread(tCls);
    2. tCls->start(QThread::NormalPriority);
    To copy to clipboard, switch view to plain text mode 

    thanks.
    Last edited by wysota; 23rd September 2013 at 14:22.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread and QTimer

    testClass should inherit QObject and not QThread, that's for sure. If your program crashes then find the crash.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QTimer

    thanks it is my mistake.

    Can you please tell me how to assing memory to timer so that it will belong to testClass thread ?
    If after moveToThread i tried to call directly using object, but i get the same error.
    But current thread id of startWork() and mainclass are different (thanks.. It is working).

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread and QTimer

    You can never call the timer directly if you want it to be part of an object living in a different thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. When should i use QThread and QTimer
    By NathiyaAmose in forum Qt Programming
    Replies: 4
    Last Post: 5th August 2013, 11:02
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. QTimer within QThread
    By Eos Pengwern in forum Qt Programming
    Replies: 6
    Last Post: 23rd February 2011, 20:00
  4. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 16:41
  5. QThread & QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 5
    Last Post: 13th April 2009, 08:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.