Results 1 to 4 of 4

Thread: Can a timer from another class be called from a thread?

  1. #1
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Can a timer from another class be called from a thread?

    I have created a class which contains a timer. The timer works fine normally when i start or stop it from the same class. but i need to use the same timer from a thread to check the data arrival in i/o of my laptop. this gives me the following error...
    QObject::killTimer: timers cannot be stopped from another thread
    QObject::startTimer: timers cannot be started from another thread

    What is the actual problem and what should i do?
    I cannot post my codes here.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can a timer from another class be called from a thread?

    Quote Originally Posted by pooch View Post
    I have created a class which contains a timer. The timer works fine normally when i start or stop it from the same class. but i need to use the same timer from a thread to check the data arrival in i/o of my laptop.
    What is the timer's purpose?

    Quote Originally Posted by pooch View Post
    QObject::killTimer: timers cannot be stopped from another thread
    QObject::startTimer: timers cannot be started from another thread
    start() and stop() are slots so the can be called through a cross-thread signal/slot connection.

    Quote Originally Posted by pooch View Post
    I cannot post my codes here.
    In such cases it is almost always possible to create a simple project that shows the same behavior.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can a timer from another class be called from a thread?

    the purpose of timer is to timeout when no signal arrives in my serial port. this timer is started within the class it is created in. but when i access the class from the thread, it shows the timer error i mentioned above. I don't access the timer directly from the thread, a function in that class does.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can a timer from another class be called from a thread?

    Quote Originally Posted by pooch View Post
    the purpose of timer is to timeout when no signal arrives in my serial port.
    And the data is read by the thread?
    Wouldn't it be better to run the timer in the same thread and either signal data arrival or timeout?
    I.e. encapsulate all in your data handler class?

    Alternatively you can run the timer in the receiver thread (probably your main thread) and just reset it in the slot that handles the data signal from your I/O thread.


    Quote Originally Posted by pooch View Post
    I don't access the timer directly from the thread, a function in that class does.
    The methods are clearly executed by the other thread, hence the error.

    Cheers,
    _

Similar Threads

  1. deleteLater called from different thread (std::thread)
    By ustulation in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2013, 12:38
  2. Replies: 0
    Last Post: 17th July 2011, 17:35
  3. how to enable a timer in a non-gui thread?
    By zeopha in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2008, 09:29
  4. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  5. Thread(s) and socket, timer slots
    By stephdev1965 in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 14:04

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
  •  
Qt is a trademark of The Qt Company.