Results 1 to 2 of 2

Thread: QThread wait for signal finishes

  1. #1
    Join Date
    Jan 2011
    Posts
    23
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread wait for signal finishes

    Hi,

    I have code similar to this
    Qt Code:
    1. void MyThread::run(void) //QThread function
    2. {
    3.  
    4.  
    5. for(int i=0;i < 100; i++)
    6. emit signal_AddTextTo_QPlainTextEdit(/* */);
    7.  
    8. //then I want to auto scroll to the top so I emit a signal
    9.  
    10. emit signal_QPlainTextEdit_scroll(QTextCursor::Start);//its not working correctly without msleep() after for loop
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 
    but its not working correctly without some delay so I use msleep(100) between signal_AddTextTo_QPlainTextEdit and signal_QPlainTextEdit_scroll.

    I dont like using msleep, it there any other solution ? wait for event (signal) finishes or something else ?

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

    Default Re: QThread wait for signal finishes

    Signals sent cross-thread are queued and executed only when the receiving thread has control and returns to its event loop: Signals and Slots Across Threads. By sleeping your thread relinquishes the CPU allowing the receiving thread to run and process the 101 queued signals. You could make the connections of type Qt::BlockingQueuedConnection or you could re-evaluate the need for a thread that appears to be doing mostly UI manipulation.

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

    ArkKup (21st November 2011)

Similar Threads

  1. Calling QThread.wait() from mainthread
    By P@u1 in forum Qt Programming
    Replies: 3
    Last Post: 1st June 2011, 15:41
  2. how to resume QThread::wait()?
    By naturalpsychic in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2011, 23:07
  3. QThread::wait() causing crash
    By Olliebrown in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2010, 15:24
  4. QThread blocking wait()
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2009, 09:21
  5. Wait for a signal
    By adonel in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 19:53

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.