PDA

View Full Version : QThread wait for signal finishes



ArkKup
20th November 2011, 20:02
Hi,

I have code similar to this


void MyThread::run(void) //QThread function
{


for(int i=0;i < 100; i++)
emit signal_AddTextTo_QPlainTextEdit(/* */);

//then I want to auto scroll to the top so I emit a signal

emit signal_QPlainTextEdit_scroll(QTextCursor::Start);//its not working correctly without msleep() after for loop

}

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 ?

ChrisW67
21st November 2011, 04:47
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.