Qt: display image during application loading

I want to add a screensaver to an application that is loading slowly. I created a simple app for testing.

main.cpp:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPixmap pixmap("/home/helene/Images/my_image.png");
    if (pixmap.isNull())
    {
        pixmap = QPixmap(300, 300);
        pixmap.fill(Qt::magenta);
    }

    QSplashScreen *splash = new QSplashScreen(pixmap);
    splash->show();
    splash->showMessage("Loaded modules dsjhj");

    QQmlApplicationEngine engine;
    engine.load(QUrl("qrc:/main.qml"));

    QObject *topLevel = engine.rootObjects().value(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    if ( !window )
    {
        qWarning("Error: Your root item has to be a Window.");
        return -1;
    }
    else
    {
        window->showFullScreen();
    }
    return app.exec();
}

main.qml

Window {
    visible: false
    width: 360
    height: 360

    property variant t: determineT()
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }

    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }

    function determineT() {
        for(var i=0; i<1000000000; i++);
    }
}

I added a long function to increase load time. When the state of the application, I see the "shadow" of the image. The image appears to be fully loaded immediately before the application. I tried with the image on the resources and with the absolute path, but the problem is the same. Picture of the "shadow"

+4
source share
1 answer

QSplashScreen , . , QApplication::processEvents() , :

QSplashScreen splash(pixmap);
splash.show();
qApp->processEvents(QEventLoop::AllEvents);

//Initialization
...

qApp->processEvents(QEventLoop::AllEvents);

//Initialization
...

qml , , . qml :

qApp->processEvents(QEventLoop::AllEvents);
+6

All Articles