Results 1 to 5 of 5

Thread: Delay initializing?

  1. #1
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Delay initializing?

    Here's my code:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MyWidget myWidget;
    5. return a.exec();
    6. }
    7.  
    8. MyWidget::MyWidget()
    9. {
    10. ...
    11. show();
    12. do_heavy_init();
    13. }
    To copy to clipboard, switch view to plain text mode 

    As above, "a.exec();" is called after "do_heavy_init();".
    Because "do_heavy_init()" costs much time, the main window would not show up immediately.
    In my case, I actually want "do_heavy_init()" to be called after myWidget is shown up on the screen. Is there an elegant way to do so? Many thanks~
    It's not the goodbye that hurts,but the flashback that follow.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Delay initializing?

    Why do you call show() in your class constructor and not in the main function?

    And on topic, it depends on what do you want to initialize, give more information.

    One idea is: add/encapsulate the data witch you initialize in and class, and do the initialization in that class constructor after you show the widget. Anyway give more info.

  3. #3
    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: Delay initializing?

    Even if you do your heavy init after the widget is shown it will be useless since then your gui is shown but frozen. So if you can put all the stuff in a thread it would be the best.

  4. #4
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delay initializing?

    Quote Originally Posted by Zlatomir View Post
    Why do you call show() in your class constructor and not in the main function?
    And on topic, it depends on what do you want to initialize, give more information.
    I think this is the same:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MyWidget myWidget;
    5. myWidget.show();
    6. return a.exec();
    7. }
    8.  
    9. MyWidget::MyWidget()
    10. {
    11. ...
    12. do_heavy_init();
    13. }
    To copy to clipboard, switch view to plain text mode 
    In do_heavy_init(), I detemine what content should be shown in the main window, before that, I can just show a loading symbol.

    Quote Originally Posted by Lykurg View Post
    Even if you do your heavy init after the widget is shown it will be useless since then your gui is shown but frozen. So if you can put all the stuff in a thread it would be the best.
    Yeah, it might freeze for a while. But what I want is to call do_heavy_init(); after myWidget is shown.


    I just checked the QSplashScreen,
    Can this ensure that myWidget is shown before do_heavy_init()? :
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MyWidget myWidget;
    5. myWidget.show();
    6.  
    7. a.processEvents();
    8. myWidget.do_heavy_init();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    It's not the goodbye that hurts,but the flashback that follow.

  5. #5
    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: Delay initializing?

    with a splash screen you can indicate that your application is loading, so it might fit your needs best. When the loading of your main application is ready, the splash is hidden and your main window is shown.
    And in the splash screen you can show messages that the user is informed, what your application is doing.

  6. The following user says thank you to Lykurg for this useful post:

    MorrisLiang (23rd August 2010)

Similar Threads

  1. Delay DLL loading ?
    By black_coder in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2009, 00:00
  2. Initializing a QList of QHashes
    By Cruz in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2009, 16:24
  3. Timer delay
    By bmn in forum Qt Programming
    Replies: 11
    Last Post: 3rd June 2008, 11:54
  4. How can i delay a function?
    By firenet in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2007, 03:58
  5. delay in qapplication
    By mandal in forum Qt Programming
    Replies: 5
    Last Post: 17th November 2006, 13:31

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.