QT program start animation

Sketch

Many of the software we use everyday has startup animation, for example, Visual Studio and PyCharm have a process of loading various components before opening the software. Their startup animation tells you that the program is opening and loading components, rather than letting you think the program is not started.

Then, can QT achieve such an effect? Of course, it can. QT provides the qsflashscreen class to achieve the effect of starting animation.  

 

Effect

 

Code path

Basic usage:

#include <QApplication>
#include <QSplashScreen>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QPixmap lodingPix("heart.png");                         //Create a picture to display for startup
    QSplashScreen splash(lodingPix);                        //Create a qsflashscreen object with pictures
    splash.show();                                          //Show this startup picture
    splash.showMessage("Love You", Qt::AlignTop|Qt::AlignRight, Qt::red);    //The first parameter is the text content, the second is the display position, and the third is the text color
    a.processEvents();                                      //Enable the program to respond to other mouse events while displaying the startup screen

    MainWindow w;
    w.show();
    splash.finish(&w);                                      //After the initialization of the main form object is completed, the startup screen is ended

    return a.exec();
}

In this way, our startup animation will appear, but the dwell time is very short. If we want to stay longer, we can handle the process of loading some things in the dwell time as follows:

#include <QApplication>
#include <QSplashScreen>
#Include < QDateTime > / / add QDateTime header file

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QPixmap lodingPix("heart.png");
    QSplashScreen splash(lodingPix);
    splash.show();
    splash.showMessage("Love You", Qt::AlignTop|Qt::AlignRight, Qt::red);
    QDateTime time = QDateTime::currentDateTime();
    QDateTime currentTime = QDateTime::currentDateTime();   //Record current time
    while (time.secsTo(currentTime) <= 5)                   //5 is the number of seconds to delay
    {
        currentTime = QDateTime::currentDateTime();
        a.processEvents();
    };

    MainWindow w;
    w.show();
    splash.finish(&w);

    return a.exec();
}

Or you can:

#include <QApplication>
#include <QSplashScreen>
#Include < qttest / qtest > / / you need to add config + = qtestlib to the pro file first

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QPixmap lodingPix("heart.png");
    QSplashScreen splash(lodingPix);
    splash.show();
    splash.showMessage("Love You", Qt::AlignTop|Qt::AlignRight, Qt::red);
    a.processEvents();
    QTest::qSleep(5000);                                    //qSleep parameter is MS

    MainWindow w;
    w.show();
    splash.finish(&w);

    return a.exec();
}

 

Keywords: Qt Pycharm

Added by Pha on Sun, 01 Dec 2019 01:32:16 +0200