+ Reply to Thread
Results 1 to 6 of 6

Thread: Splash screen showing for two seconds

  1. #1
    Join Date
    Sep 2008
    Posts
    13
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Splash screen showing for two seconds

    Hi, I've searched for this and haven't found it, so I thought I'd share this little code.

    If you want a dummy splash screen (that is, your main window can start immediately but you want the splash screen to stay a couple of seconds) you can use the following code:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QSplashScreen>
    3. #include "mainWND.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. QPixmap pixmap(":/mainWND/Resources/splash.png");
    9. QSplashScreen splash(pixmap,Qt::WindowStaysOnTopHint);
    10. mainWND w;
    11. w.setWindowOpacity(0);
    12. w.show();
    13. splash.show();
    14. QTimer::singleShot(2000, &splash, SLOT(close()));
    15. QTimer::singleShot(2000, &w, SLOT(slInit()));
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    And define a slot in your main window called slInit (or whatever you want to call it) where you put the following code:

    Qt Code:
    1. void mainWND::slInit()
    2. {
    3. setWindowOpacity(1);
    4. }
    To copy to clipboard, switch view to plain text mode 

    With this trick you can show your splash screen for some seconds, then make your main window appear. I have only tested it in Windows Vista, any feedback for other platforms would be great.

    I hope this helps anyone, this forum has been a great source of info for my Qt programming and I thought I should give something back!

  2. #2
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Splash screen showing for two seconds

    you can also add some comments during this time
    Qt Code:
    1. pSplash->showMessage("Start ...");
    2. pSplash->showMessage("Initialise the Database...");
    To copy to clipboard, switch view to plain text mode 
    replace "->" by "." in your case
    Last edited by SunnySan; 17th October 2008 at 15:05.

  3. #3
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Splash screen showing for two seconds

    to do what you have to create an header for main.h ?? isn't it?

    oups forget it....
    You added the call to the mainwindow. read it too quickly.

  4. #4
    Join Date
    Sep 2008
    Location
    New York
    Posts
    90
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Splash screen showing for two seconds

    under my X11, the splashwindow comes with the mainwindow.
    Here is the description in QWidget class

    "This feature is available on Mac OS X, X11 platforms that support the Composite extension, and Windows 2000 and later.

    Note that under X11 you need to have a composite manager running, and the X11 specific _NET_WM_WINDOW_OPACITY atom needs to be supported by the window manager you are using."

    but you can simply change "w.setWindowOpacity(0);w.show();" to "w.hide()"
    can redefine slinit function to "this->show()" and works as expected.


    Quote Originally Posted by Koas View Post
    Hi, I've searched for this and haven't found it, so I thought I'd share this little code.

    If you want a dummy splash screen (that is, your main window can start immediately but you want the splash screen to stay a couple of seconds) you can use the following code:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QSplashScreen>
    3. #include "mainWND.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. QPixmap pixmap(":/mainWND/Resources/splash.png");
    9. QSplashScreen splash(pixmap,Qt::WindowStaysOnTopHint);
    10. mainWND w;
    11. w.setWindowOpacity(0);
    12. w.show();
    13. splash.show();
    14. QTimer::singleShot(2000, &splash, SLOT(close()));
    15. QTimer::singleShot(2000, &w, SLOT(slInit()));
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    And define a slot in your main window called slInit (or whatever you want to call it) where you put the following code:

    Qt Code:
    1. void mainWND::slInit()
    2. {
    3. setWindowOpacity(1);
    4. }
    To copy to clipboard, switch view to plain text mode 

    With this trick you can show your splash screen for some seconds, then make your main window appear. I have only tested it in Windows Vista, any feedback for other platforms would be great.

    I hope this helps anyone, this forum has been a great source of info for my Qt programming and I thought I should give something back!

  5. #5
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Splash screen showing for two seconds

    thanks
    I modified my splash screen
    et voila working code below

    Qt Code:
    1. int main(int argc, char *argv[]) {
    2. QApplication a(argc, argv);
    3. a.connect(&a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );// added test for player
    4.  
    5. // The splash screen
    6. QSplashScreen *pSplash = new QSplashScreen();
    7. pSplash->setPixmap(QPixmap(":images/Splash.png"));
    8. pSplash->show();
    9. pSplash->showMessage("Start ...");
    10.  
    11. // main window
    12. MainWindow w;
    13. w.setWindowTitle(QObject::tr(" my title"));
    14. w.hide();// mainwindow disappears
    15.  
    16. QTimer::singleShot(3000, pSplash, SLOT(close()));// close splash after 4s
    17. QTimer::singleShot(3000, &w, SLOT(slInit()));// mainwindow reappears after 4s
    18.  
    19.  
    20. // create DataBase.
    21. pSplash->showMessage("Initialise the Database...");// shows comments
    22. w.databaseobject-> createPlayListTables(); // DB created but need another object in mainwindow!!
    23.  
    24. pSplash->showMessage("load data...");
    25. w.loadProgamListFromDB(); //load data to application
    26.  
    27. return a.exec();
    28.  
    29. }
    30.  
    31. also into the mainwindow....
    32. ....
    33.  
    34. void mainWND::slInit(){
    35. this->show();
    36. }
    37. .......
    To copy to clipboard, switch view to plain text mode 

    thanks
    pass the code around

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

    td (22nd October 2008)

  7. #6
    Join Date
    Sep 2008
    Posts
    13
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Splash screen showing for two seconds

    Quote Originally Posted by Sheng View Post
    but you can simply change "w.setWindowOpacity(0);w.show();" to "w.hide()"
    can redefine slinit function to "this->show()" and works as expected.
    Of course! How didn't I think of that! Maybe it's because I was playing with the setWindowOpacity() function and thought about the splash screen issue...

    Anyway, the "hide() then show()" solution is much better, since we don't have to worry about the transparency issues (I think my first code wouldn't work well in Windows 98 for example).

    I love these forums, everyday I learn something new!

+ Reply to Thread

Similar Threads

  1. Using QGraphicsView as a Splash Screen (repaint issues)
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2008, 22:22
  2. QTimer based Splash Screen
    By bpetty in forum Newbie
    Replies: 6
    Last Post: 15th December 2006, 01:51

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