Results 1 to 5 of 5

Thread: QThread and QTimer

  1. #1
    Join Date
    Jul 2008
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread and QTimer

    Hi there,

    i have a QTimer conflict within a ClassA inheriting from QThread. ClassA has a massive data processing routine, which we call from the QThread::run() in order to let it run with the Qt::LowPriority. This works fine. ClassA has a single instance during the whole runtime of the application.


    Qt Code:
    1. class ClassA : QThread
    2. {
    3. public:
    4. ClassA();
    5. ~ClassA();
    6. InitGUI();
    7. focusHasLeft();
    8. focusHasCome();
    9.  
    10. protected slots:
    11. void refreshTimerSlot(); // this eventually starts the thread for reload of data
    12. threadFinished(); // this slot is connected to the "thread is finished" signal
    13.  
    14. private:
    15. //start the thread with low priority.
    16. startThread();
    17.  
    18. // QThread
    19. void run(void); // this calls massiveDataReloadRoutine()
    20. void massiveDatareloadRoutine();
    21.  
    22. QTimer refreshTimer;
    23. }
    To copy to clipboard, switch view to plain text mode 

    ClassA has a refresh timer which is used update the GUI periodically. This timer is in conflict with the QThread giving the message

    "QObject::startTimer: timers can not be started from another thread"
    inside the debug window. As a result, I can not start and stop the timer, and it runs all the time. The timer slot is being called without a problem.



    But the problem is, if a change focus from the view i want to stop the timer so that the updates should not steal processing time.

    In order to to stop the timer i have tried:

    1) to stop the timer in ClassA::run() -> did not work
    2) to stop the timer in ClassA::focusHasLeft() -> did not work
    3) to stop the timer in ClassA::threafFinished() -> did not work

    What is wrong?
    Last edited by jpn; 1st May 2009 at 11:02. Reason: missing [code] tags

  2. #2
    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

    QObject affinity is to the thread that created the object. Therefore QThread (or its subclass) object and all its member variables belong to the thread that created that object and not to the thread the object controls. So you can't start/stop the timer that is a member variable of the class from within the run() method unless you either create the timer within the run() method or you change the affinity of the QThread object by using QObject::moveToThread().
    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.


  3. #3
    Join Date
    Jul 2008
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread and QTimer

    I have forgotten to mention that i have tried the moveToThread() solution. It did not work.

    Since yesterday, i have redesigned the thread, defined new signals, but i receive a compiler error (C2594 ambiguous conversion) during compilation of the moc file.

    The error points to the following line in moc_ClassA.cpp:

    Qt Code:
    1. void ClassA::signal1()
    2. {
    3. ERROR-> QMetaObject::activate(this, &staticMetaObject, 1, 0);
    4. }
    To copy to clipboard, switch view to plain text mode 

    i can not use the suggested (by msdn) solution: "to use reinterpret_cast or static_cast explicitly", because i do not want to edit the moc file.


    Qt Code:
    1. class ClassA : public QThread, public GUIWrapperClass, ...
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

    where GUIWrapperClass inherits from QObject.

    Is this the reason for the ambiguity? Now ClassA inherits both from QThread and GUIWrapperClass to QObject.

    I HAVE TO USE GUIWrapperClass so that the ClassA works fine with our framework.

    is there something one can do?
    Last edited by jpn; 1st May 2009 at 11:03. Reason: missing [code] tags

  4. #4
    Join Date
    Jul 2008
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread and QTimer

    I tried somthing like this, the problem is still the same...




    Qt Code:
    1. class ThreadWrapper: public QThread
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. public:
    7. ~ThreadWrapper();
    8.  
    9. signals:
    10. virtual void reloadFinished(void) = 0;
    11. virtual void stopVisualizationTimer(void) = 0;
    12. virtual void reloadData(double requiredStartTime) = 0;
    13.  
    14. }
    15.  
    16. class ClassA : public ThreadWrapper, public GUIWrapperClass
    17. {
    18.  
    19. signals:
    20. void reloadFinished(void);
    21. void stopVisualizationTimer(void);
    22. void reloadData(double requiredStartTime);
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 1st May 2009 at 11:03. Reason: missing [code] tags

  5. #5
    Join Date
    Jul 2008
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread and QTimer

    Compilation problem was solved using this:


    Qt Code:
    1. class ThreadWrapper: public QThread
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. public:
    7. ~ThreadWrapper();
    8.  
    9. signals:
    10. void reloadFinished(void);
    11. void stopVisualizationTimer(void);
    12. void reloadData(double requiredStartTime);
    13.  
    14. }
    15.  
    16. class ClassA : public ThreadWrapper, public GUIWrapperClass
    17. {
    18.  
    19. //signals:
    20. //void reloadFinished(void);
    21. //void stopVisualizationTimer(void);
    22. //void reloadData(double requiredStartTime);
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 1st May 2009 at 11:03. Reason: missing [code] tags

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. QTimer in QThread doesn't start or timeout
    By Boron in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2011, 13:51
  3. QThread & QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 5
    Last Post: 13th April 2009, 08:22
  4. QTimer and QThread in Qtopia 4.2.0
    By mellibra in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 25th October 2007, 08:26
  5. QTimer and QThread
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2006, 14:52

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.