PDA

View Full Version : main loop



dusza
5th June 2009, 11:11
Hi, I want to communicate with server to receive information. I do it in infinite while loop in other thread but I can't use threads so I have to include the thread's code somewhere into MainWindow Class. I've tried below showing main window function but the application doesn't want to show the form. I don't know is there something like main loop in qt because i could not find it in google.



QApplication a(argc, argv);
MainWindow w;
w.show();

while(1){}

return a.exec();

Lykurg
5th June 2009, 11:21
but I can't use threads so I have to include the thread's code somewhere into MainWindow Class.

Then use a timer which triggers a function each second.

dusza
5th June 2009, 11:41
I would not rather use timers, maybe some other solution?

spirit
5th June 2009, 11:55
of course your window will not be shown, because you enter in infinate loop before calling app.exec, try to rewrite code like this


QApplication a(argc, argv);
MainWindow w;
w.show();
bool res = a.exec();
while(1){}

return res;

but, anyway, this is useless code.
take a look at QEventLoop.
btw why you can't use threads?

wysota
5th June 2009, 13:26
What's wrong with timers?

dusza
5th June 2009, 23:20
It's a bit complicated why I can't use threads but finally I figured out that I will not handle without them :p Timers are good but what about situation e.g. I have a function that stops program for some period of time waiting for response and with time interval set to 0 it can spoil the application I suppose, can't it?

wysota
6th June 2009, 00:12
Yes, but it will break it regardless if you use timers or not. Provided that you actually do something with the data you exchange with the server blocking the main thread will block your other functionality as well. Timers are the best approach in your case - threads will just make your application more complicated giving nothing in exchange.

dusza
6th June 2009, 14:04
Yes but in the other thread I'm just listening for data while in the main program I'm sending data to server. I think that this approach is better and easier than timers but I can be wrong.

wysota
6th June 2009, 15:14
Do you do anything with the data you receive?

dusza
6th June 2009, 15:28
No, I'm just printing it on a form. But I've tried it, I set timer interval to 0 and in trigged function I've inserted a socket recv function, which waits for data from server, but then I can't use a form because the application is suspend.

wysota
6th June 2009, 17:29
No, I'm just printing it on a form.
You need the main thread for that. If you block it, you won't see anything. So the argument against using timers because of a potential block is a killer also when using threads.


But I've tried it, I set timer interval to 0
What for?


and in trigged function I've inserted a socket recv function, which waits for data from server, but then I can't use a form because the application is suspend.

Use signals and slots. You don't need neither timers nor threads for accessing sockets.