Results 1 to 2 of 2

Thread: qthread - eventloop - in non-qt app

  1. #1
    Join Date
    Feb 2010
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default qthread - eventloop - in non-qt app

    Hi

    I try to use a simple thread
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. Q_OBJECT
    4. private:
    5. virtual void run()
    6. {
    7. QTimer timer;
    8. QObject::connect(&timer, SIGNAL(timeout), this, SLOT(onTimer()));
    9. timer.start(1000);
    10. QThread::exec();
    11. }
    12. private slots:
    13. void onTimer() {....}
    14. };
    To copy to clipboard, switch view to plain text mode 
    in an application that does not have a QCoreApplication event loop in the main thread, e.g.
    Qt Code:
    1. void main()
    2. {
    3.  
    4. //no app.exec() here, we use our custom loop
    5.  
    6. forever()... {}
    7. }
    To copy to clipboard, switch view to plain text mode 
    I start an instance of MyThread but I never receive any timer events.
    I am not able to add app.exec() in the main thread so I added a QEventLoop somewhere and then I started to receive my timer events.
    But I don't understand what is going on: I expected to have the timer events working since I do have an event loop inside my thread.

    I cannot check now, but it just came to me that maybe the onTimer() slot gets called on a different thread.
    Can that be the problem?

    Can somebody please explain?
    Thanks a lot
    Last edited by anda_skoa; 23rd January 2015 at 11:22. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qthread - eventloop - in non-qt app

    The line
    Qt Code:
    1. QObject::connect(&timer, SIGNAL(timeout), this, SLOT(onTimer()));
    To copy to clipboard, switch view to plain text mode 
    connects the timer's signal to a slot of the QThread object itself. The QThread object lives in the main thread, not in the thread it manages. Therefore the connection behaves as a queued connection, and the slot will only be executed if you start the event loop in the main thread.

    You could instead move the slot to a new object, e.g. MyObject, that lives in the worker thread, e.g.:
    Qt Code:
    1. class MyObject : public QObject {
    2. Q_OBJECT
    3. /* ... */
    4. public slots:
    5. void onTimer();
    6. }
    7.  
    8. /* ... */
    9.  
    10. virtual void MyThread::run() {
    11. QTimer timer;
    12. MyObject myObject;
    13. QObject::connect(&timer, SIGNAL(timeout), &myObject, SLOT(onTimer()));
    14. timer.start(1000);
    15. QThread::exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

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

    ctarsoaga (23rd January 2015)

Similar Threads

  1. Eventloop does not quit properly
    By MrAnderson1983 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2014, 10:31
  2. crash on qt eventloop
    By waiter in forum Newbie
    Replies: 1
    Last Post: 28th April 2013, 11:28
  3. QTcpSocket EventLoop thread is still running?
    By mihau in forum Qt Programming
    Replies: 0
    Last Post: 28th February 2011, 09:41
  4. Problem with eventloop in QThread
    By speedy_gonzales in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2010, 19:17
  5. Thread eventLoop and run
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 19:36

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.