mardi 17 mars 2020

QSplashScreen disappears too quickly

I am trying to show a QSplashScreen before launching my application. The problem I have is that the QSplashScreen disappears too quickly. I would like it to show for 2 seconds before the real application loads. Below the small example I built to show the error:

#include <QApplication>
#include <QSplashScreen>
#include <QTimer>

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

    QSplashScreen *splash = new QSplashScreen;
    splash->setPixmap(QPixmap(":/dredgingSplash.png"));
    splash->show();
    Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
    splash->showMessage(QObject::tr("Setting up the main window..."), topRight, Qt::white);


    QTimer::singleShot(2500, splash, SLOT(close()));

    a.setApplicationName(APPNAME);
    a.setApplicationVersion(APPVER);
    a.setOrganizationName(ORGNAME);
    a.setStyle("fusion");
    MainWindow w;
    QTimer::singleShot(2500, &w, SLOT(show()));

    splash->showMessage(QObject::tr("loading modules..."), topRight, Qt::white);
    splash->showMessage(QObject::tr("Establishing Connections..."), topRight, Qt::white);

    w.show();
    delete splash;
    return a.exec();
}

I tried to play with the QTimer as I thought that could be the potential problem due to priority of loading, but unfortunately if I change QTimer::singleShot(2500, splash, SLOT(close())); with QTimer::singleShot(12500, splash, SLOT(close())); or even higher numbers, there is literally no difference, so I am not sure why that is not an option.

Also I came across this source and I also followed official documentation but that also did not help me to figure out the problem. Also this post suggests that the problem keeps being related to the QTimer but I don't see how since bigger or smaller intervals seems not to count.

What am I missing to keep the QSplashScreen to stay for 2 seconds instead of disappearing (or not even see it) right away? Thanks for pointing to the right direction.

Aucun commentaire:

Enregistrer un commentaire