Results 1 to 5 of 5

Thread: how to emit from thread

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default how to emit from thread

    emits included in customized thread inside my program does not work. it seems my thread has no communication with the main UI thread. in c# we have thread dispatcher to handle this. what about QT?

    Qt Code:
    1. void myThread::run(){
    2.  
    3. while(1)
    4. {
    5. //do something
    6. emit display("show the result"); //NOT WORKING, THOUGH CONNECTED
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 20th April 2013 at 15:24.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to emit from thread

    Qt signals operate between threads. Show some code.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to emit from thread

    does this work?
    Qt Code:
    1. void myThread::run(){
    2.  
    3. while(1)
    4. {
    5. //do something
    6. emit display("show the result"); //NOT WORKING, THOUGH CONNECTED
    7. msleep(1);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to emit from thread

    In standard multithread connection signal and slot are connected in Qt::QueuedConnection mode. To work this it must be working event loop in thread. In your case there is no working event loop. Try change code to something like this :
    Qt Code:
    1. void myThread::run()
    2. {
    3. QTimer::singleShot( 0,this,SLOT(doSomethingOne()));
    4. QThread::run();
    5. }
    6.  
    7. void myThread::doSomethingOne()
    8. {
    9. //do something
    10. emit display("show the result"); //NOW IS WORKING :)
    11. QTimer::singleShot( 0,this,SLOT(doSomethingOne()));
    12. }
    To copy to clipboard, switch view to plain text mode 
    Standard QThread::run method starts event loop.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to emit from thread

    Quote Originally Posted by Lesiok
    In standard multithread connection signal and slot are connected in Qt::QueuedConnection mode. To work this it must be working event loop in thread. In your case there is no working event loop.
    Event loop is required only to execute the slot connected in Qt::QueuedConnection type, event loop is not required for the object to emit signal. (not even in different thread)
    Last edited by Santosh Reddy; 20th April 2013 at 17:27.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Emit Signal to Thread and additional Forward
    By CLRS530 in forum Qt Programming
    Replies: 0
    Last Post: 23rd May 2010, 20:45
  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

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.