Search code examples
c++qtqt5splash-screen

How to hide QMainWindow and show splashcreen during startup?


I am trying to hide the MainWindow of my Qt desktop app during startup, and to show a splashscreen. Both only happens after the loading phase, even though I call both splash.show() and window.hide() before the loading phase. I tried to split loading phase and constructor, but result is the same. How can I achieve both before the loading phase ?

Update 1

To display the splash screen, I had to add a call to QApplication::processEvents()

Update 2

The black window was actually not the MainWindow, but a ghost window that popped because scrollArea->setVisible(true) was called in the constructor.

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

    QPixmap pixmap(QStringLiteral(":/ressources/icons/icon.png"));
    QSplashScreen splash(pixmap);
    splash.show();

    MainWindow window; // this loads for 5-6 seconds
    a.processEvents();
    window.showLoginPrompt();
    splash.finish(&window);

    return a.exec();
}

Solution

  • Based on your code and some example I could make it run like you are trying to do. You only need to call your promptLogin function instead.

    
        #include <QApplication>
        #include <QTimer>
        #include <QSplashScreen>
        #include "mainwindow.h"
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            QSplashScreen *splash = new QSplashScreen;
            splash->setPixmap(QPixmap("D:\\Projects\\SplashScreen\\TestSplashScreen\\splash.png"));
            splash->show();
        
            MainWindow mainWin;
        
            QTimer::singleShot(2500, splash, SLOT(close()));
            QTimer::singleShot(2500, &mainWin, SLOT(show()));
        
            return app.exec();
        }