Results 1 to 3 of 3

Thread: emit singnals from another thread

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default emit singnals from another thread

    Hi everybody!

    I want to know if I emit a signal from another thread which event loop is responsible for emitting the signal. in other words, this signal emitted from which thread?

    for example mysignal in below code
    Qt Code:
    1. class O2;
    2.  
    3. class O1 : public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. O1(QObject *parent = 0): olocal(new O2(this)), QObject(parent)
    8. {
    9. m_thread = new QThread;
    10. olocal->moveToThread(m_thread);
    11. connect(m_thread, SIGNAL(started()), olocal, SLOT(start()));
    12. connect(olocal, SIGNAL(finished()), m_thread, SLOT(quit()));
    13.  
    14. ....
    15. }
    16.  
    17. ~O1() {
    18. olocal->finish();
    19. m_thread->wait();
    20. delete olocal;
    21. delete m_thread;
    22. }
    23.  
    24. signals:
    25. void mysignal();
    26. };
    27.  
    28. class O2 : public QObject
    29. {
    30. Q_OBJECT
    31. public:
    32. O2(O1 *parent) : q_ptr(parent) {}
    33.  
    34. signals:
    35. void finished();
    36.  
    37. public slots:
    38. void start() { b_start = true; QTimer::singleShot(0, this, SLOT(run())); }
    39. void finish() { b_finish = true; }
    40.  
    41. void run() {
    42. if(b_finished) {
    43. emit finished();
    44. return;
    45. }
    46.  
    47. // do some staff
    48. if( /* something */)
    49. emit q_ptr->mysignal();
    50.  
    51. if(b_start)
    52. QTimer::singleShot(0, this, run());
    53. }
    54. };
    To copy to clipboard, switch view to plain text mode 

    thanks in advance
    Last edited by danics; 7th March 2017 at 12:00.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: emit singnals from another thread

    The signal would be emitted from the thread that is executing your code that does the emit. Your connect statements above will used queued connections when the sender/receiver are in different threads. You can force the connection type, but I would recommend that you use the default queued connection type unless you have very specific requirements.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

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

    danics (8th March 2017)

  4. #3
    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: emit singnals from another thread

    Just to clarify: there is no event loop involved with emitting
    However, with a QueuedConnection the slot it called by the event loop of the thread "owning" the receiver object.

    The same is true for an AutoConnection when the thread executing the emit is a different one than the "receiver" thread, as jefftee said.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    jefftee (9th March 2017)

Similar Threads

  1. how to emit from thread
    By saman_artorious in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2013, 17:17
  2. Emit signal from thread
    By sisco in forum Newbie
    Replies: 2
    Last Post: 26th November 2009, 14:32
  3. emit is slow in Thread?
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2007, 09:52
  4. QT emit/signaling thread-safe?
    By richy in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2006, 10:37
  5. Replies: 2
    Last Post: 6th January 2006, 22:15

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.