Results 1 to 2 of 2

Thread: How to start a QTimer in this situation?

  1. #1
    Join Date
    Sep 2020
    Posts
    1
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question How to start a QTimer in this situation?

    Qt Code:
    1. class Foo {
    2. private:
    3. QTimer *timer = nullptr;
    4.  
    5. public:
    6. void Foo (QTimer *timer) : timer(timer) { }
    7.  
    8. void bar () {
    9. this->timer->start();
    10. }
    11. };
    12.  
    13. ...
    14.  
    15. QTimer *timer = new QTimer();
    16. Foo *foo = new foo(timer);
    17.  
    18. obj.func(foo);
    19. timer->connect(timer, &QTimer::timeout, [] {
    20. // do something
    21. });
    To copy to clipboard, switch view to plain text mode 

    The program runs in the following order:
    1. a third party library creates a new thread.
    2. foo object is registered in the library through its obj::func() method.
    3. the library calls foo::bar() method.

    But after that, the program doesn't work with error message
    QObject::startTimer: Timers cannot be started from another thread.

    Could you give me some advice if you have a good idea? Thanks.
    Last edited by iaapmm; 28th September 2020 at 04:53.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,668
    Thanks
    13
    Thanked 1,595 Times in 1,523 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to start a QTimer in this situation?

    Try this...
    Give the Foo class a timeout() signal.
    Create the QTimer object inside Foo (pass in a period etc.): hopefully you end up with the timer in the same thread as the Foo object.
    Connect the timer's timeout() signal to Foo's timeout() signal. This will cause your foo object to emit timeout() when the internal timer emits timeout().

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

    iaapmm (28th September 2020)

Similar Threads

  1. start QTimer form C#
    By bibhukalyana in forum Qt Programming
    Replies: 16
    Last Post: 10th October 2013, 12:47
  2. Replies: 1
    Last Post: 25th October 2012, 19:47
  3. QTimer unnestable with every time i stop/start it
    By aguayro in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2012, 17:55
  4. Replies: 2
    Last Post: 11th February 2011, 19:48
  5. QTimer don't start
    By Qn00b in forum Qt Programming
    Replies: 13
    Last Post: 7th February 2011, 23:02

Tags for this Thread

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.