Results 1 to 11 of 11

Thread: main loop

  1. #1
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default main loop

    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.

    Qt Code:
    1. QApplication a(argc, argv);
    2. MainWindow w;
    3. w.show();
    4.  
    5. while(1){}
    6.  
    7. return a.exec();
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 5th June 2009 at 13:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: main loop

    Quote Originally Posted by dusza View Post
    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.

  3. #3
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: main loop

    I would not rather use timers, maybe some other solution?

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: main loop

    of course your window will not be shown, because you enter in infinate loop before calling app.exec, try to rewrite code like this
    Qt Code:
    1. QApplication a(argc, argv);
    2. MainWindow w;
    3. w.show();
    4. bool res = a.exec();
    5. while(1){}
    6.  
    7. return res;
    To copy to clipboard, switch view to plain text mode 
    but, anyway, this is useless code.
    take a look at QEventLoop.
    btw why you can't use threads?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: main loop

    What's wrong with timers?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: main loop

    It's a bit complicated why I can't use threads but finally I figured out that I will not handle without them 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?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: main loop

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: main loop

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: main loop

    Do you do anything with the data you receive?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: main loop

    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.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: main loop

    Quote Originally Posted by dusza View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. HAL & DBusConnection & Qt main loop integration
    By juannm in forum Qt Programming
    Replies: 3
    Last Post: 15th January 2009, 08:06
  2. Main Thread Event loop
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 12:10
  3. Replies: 4
    Last Post: 10th March 2007, 18:01
  4. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55
  5. While statement inside the main loop
    By OnionRingOfDoom in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2006, 18:17

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.