Results 1 to 8 of 8

Thread: QTimer in QThread working in ubuntu but not working in windows

  1. #1
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question QTimer in QThread working in ubuntu but not working in windows

    Qtimer timeout slot is executing in linux but not working in windows showing

    QObject::startTimer: timers cannot be started from another thread

    help me if anybody knows. Thanks in advance....
    Attached Files Attached Files

  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: QTimer in QThread working in ubuntu but not working in windows

    Create the timer in the run() method of the 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.


  3. The following user says thank you to wysota for this useful post:

    pradeepreddyg95 (24th September 2012)

  4. #3
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in QThread working in ubuntu but not working in windows

    hi wysota, I tried that method also same warning is coming but my confusion is same thing is working under ubuntu can u plz check the sample test code ...

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTimer in QThread working in ubuntu but not working in windows

    Two things you are missing

    1. Event loop is not being ececuted. (i.e exec(), this is required, else the thread will exit)
    2. Timer should started from the parent thread , i.e the ctor() and run() functions execute in different thread context hence the object created on ctor() should be moved the the the thread, or other way is to create the timer in run() method (as wysota said)
    Attached Files Attached Files
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    pradeepreddyg95 (24th September 2012)

  7. #5
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in QThread working in ubuntu but not working in windows

    Thanks Santosh,
    But in ubuntu i am using like this
    void Thread::run()
    {
    while(1)
    {
    // some operations doing inside the loop
    if(m_pProbeFailure[i].Value > m_nCuurrent_ADC_Value)
    {
    if(!m_pTimer[i].isActive())
    m_pTimer[i].start();
    }

    }

    }
    like this iam using without calling exec() it working fine but in windows not working ... Plz solve my issue .... if any body ...

  8. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTimer in QThread working in ubuntu but not working in windows

    QTimer will works when you have have a event loop (i.e. call exec());, or other way is to explictily process the events by calling QCoreApplication:rocessEvents(), from the local while loop.

    Qt Code:
    1. void Thread::run()
    2. {
    3. while(1)
    4. {
    5. // some operations doing inside the loop
    6. if(m_pProbeFailure[i].Value > m_nCuurrent_ADC_Value)
    7. {
    8. if(!m_pTimer[i].isActive())
    9. m_pTimer[i].start();
    10. }
    11. QCoreApplication::processEvents();
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. The following user says thank you to Santosh Reddy for this useful post:

    pradeepreddyg95 (24th September 2012)

  10. #7
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTimer in QThread working in ubuntu but not working in windows

    Thanks Santosh its working .....


    Added after 16 minutes:


    hi Santosh But iam unable to stop timers after timeout showing the following msg

    void Thread::TimerTimeOut()
    {
    qDebug() << "TimerTimeOut " << sender()->objectName();

    QTimer *pTimer = qobject_cast<QTimer*>(sender());
    pTimer->stop();
    }

    TimerTimeOut "Timer1"
    QObject::killTimer: timers cannot be stopped from another thread
    Last edited by pradeepreddyg95; 24th September 2012 at 07:27.

  11. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTimer in QThread working in ubuntu but not working in windows

    You cannot create the timer in one thread and operate it from another: there is no new error or problem here and the error message tells you exactly where the problem lies.
    • If the timer exists in the main thread then it can be started and stopped from code executing in the main thread only.
    • If the timer exists in the a secondary thread then it can be started and stopped from code executing in the secondary thread only.
    • Same goes for the third, fourth, fifth ...

    You ask us to solve your problem when you have failed to take on board the information already supplied.

    In your original code: code in the Thread class constructor executes in the main thread creating a timer in that thread. Code in the run() function runs in the controlled thread. Thus the pTimer is in one thread, and the pTimer->Start() is in another and never the two shall meet.
    Last edited by ChrisW67; 24th September 2012 at 08:22.

  12. The following user says thank you to ChrisW67 for this useful post:

    pradeepreddyg95 (24th September 2012)

Similar Threads

  1. Qtimer is not working....
    By k.qasempour in forum Newbie
    Replies: 3
    Last Post: 21st June 2012, 09:16
  2. Replies: 1
    Last Post: 8th August 2011, 16:04
  3. Working with MYSQL in ubuntu
    By zero-n in forum Newbie
    Replies: 1
    Last Post: 26th July 2010, 22:02
  4. QTimer - Why this is not working?
    By prasenjit in forum Newbie
    Replies: 2
    Last Post: 17th December 2009, 17:37
  5. Working with QThread
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2009, 23:35

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.