How to control the events of the main event loop of Qcoreapplication
I use a Qcoreapplication in the main function of a program. I have a sequence of communicating nodes which is read from a file (line by line). At each line, I call sendHello(src,dst). A simple description of the code is as follows:
file format:
node1 node2
node3 node4
node5 node6
Code:
int main()
{
Qcoreapplication app;
readfromfile();
app.exec()
}
readfromfile()
{
while(!eof())
sendhello(src,dst),
}
sendHello(src,dst)
{
// Here exchange different messages (more events)
sendmsg2(dst,src)
sendmsg3(src,dst)
}
Calling each of this sendHello() by each pair, initiates different exchange of information (different events). I found out that the main loop goes from line to line (from pair to pair) and performs the sendHello for each pair sequentially.
Results I get:
sendHello(node1,node2)
sendHello(node3,node4)
sendmsg2(node2,node1)
sendmsg3(node1,node2)
sendmsg2(node4,node3)
sendmsg3(node2,node4)
I need to execute the events that are contained in each sendHello (sendmsg2 and sendmsg3) before moving to next lines (pairs). I tried to understand how to control the events of the main event loop, and how the middle events are scheduled but I did not.
Results I need:
sendHello(node1,node2)
sendmsg2(node2,node1)
sendmsg3(node1,node2)
sendHello(node3,node4)
sendmsg2(node4,node3)
sendmsg3(node2,node4)
I tried processevents( all events) but it`s not working as required. Don`t know why ?
Thanks in advance
Re: How to control the events of the main event loop of Qcoreapplication
You need to start the event loop before you start reading the nodes. Moreover you can't read the nodes in a while loop because you will block the event loop. You can use QTimer::singleShot() with a 0 timeout to schedule execution of a slot without blocking events. You can read more about similar approaches in the article on [wiki]Keeping the GUI Responsive[/wiki].
Re: How to control the events of the main event loop of Qcoreapplication
Thank you so much , the article you provided me is very useful and I could solve the problem. Before any long operation , I created an event loop, and the event loop starts execution till it gets a signal (timer with 0 time out) that this long operation is finished.