Qt Splash Screen

Qt Splash Screen

Having a beautiful splash screen can give your program some class. Impress some of your clients as well if you use Qt to program. I'll show you how to edit the Splash Screen message, program the Splash screen, and play with the images.

If you look at the screenshot provided, you can see what we can create using splash screen. Splashscreens are only beneficial to large programs and programs that need to load a lot of modules or files. It usually won't look good for small programs that load pretty quick.

Using the class QSplashScreen we can create our splash screen and play around with it.

Our qt main cpp file:

#include <QtGui>
#include <windows.h>

int main(int argc, char *argv[]) {
        QApplication App(argc, argv);
        QPixmap pix(":/twirl.png");
       
        QSplashScreen splash(pix);
        splash.show();
        splash.showMessage("Loading Processes");
        App.processEvents();
        splash.showMessage("Loading Modules");
        //Sleep(10000);
        // do some work with your classes
        splash.showMessage("Ready");
       
        QMainWindow window;
        QLabel ql(&window);
        ql.setPixmap(pix);
        window.setCentralWidget(&ql);
    window.show();
        ql.show();
        //Sleep(10000);
    //splash.finish(&window);
       
        return App.exec();
}

We have created a project file and a resource file, to include a twirl.png (do not try and include JPG, as Pixmaps don't allow it).

The above code is easy to understand, we create our application. Then we create a QPixmap or an image, grab the image from our resource using the colon. Then we create a QSplashScreen, assign the pixmap to it. Then we show the SplashScreen, process our application events, then we can also show messages using ShowMessage. We also create a label that has our image as well so that we know when the program is loaded.

I have commented out the Sleep (used in windows.h and win32 programs) and the finish so that we can actually see our splashscreen before it disappears. In a normal program you'd need to finish() the splashscreen so that once your main window class is loaded and all your program files are ready, you can end the splash screen.

Of course then we run the whole thing using app.exec().

Here's how our project file and resource file looks in case you have had any trouble with that. QtTest.pro :

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
RESOURCES = res.qrc

# Input
SOURCES += qtmain.cpp

res.qrc, our Qt Resource File.

<RCC>
<qresource prefix="/">
    <file>twirl.png</file>
</qresource>
</RCC>

You may also use QTimer::singleShot(10000, &splash, SLOT(close())); to have some interesting splashscreens even with tiny programs! (Or use Sleep; though that isn't recommended)

chilarai's picture

nice one man

nice one man

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.