Results 1 to 3 of 3

Thread: emit singnals from another thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 11:00.

Similar Threads

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