How to create a splash screen with an induced delay.

We have QSplashScreen [doc.qt.nokia.com] which is used to cover up the starting delay of the program. More about splash screen is here [doc.qt.nokia.com].
Some times the programs might be quick enough ,so that the splash screen may not be visible. We may induce some delay to show the splash , as a decoration !

Here in this example, using QThread [doc.qt.nokia.com] a delay is induced.

  1. #include <QApplication>
  2. #include <QSplashScreen>
  3. #include <qthread.h>
  4. #include "mainwindow.h"
  5.  
  6.  
  7. class I : public QThread
  8. {
  9. public:
  10.         static void sleep(unsigned long secs) {
  11.                 QThread::sleep(secs);
  12.         }
  13. };
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.  
  18.     QApplication app(argc, argv);
  19.     QPixmap pixmap("splash.jpg");
  20.     QSplashScreen splash(pixmap);
  21.     splash.show();
  22.     MainWindow mainWin;
  23.     mainWin.setWindowTitle("Application");
  24.     I::sleep(5);               // splash is shown for 5 seconds
  25.     mainWin.showMaximized();
  26.     splash.finish(&mainWin);
  27.     return app.exec();
  28. }

Easy! Happy coding!

Categories: