Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Splashimage freezed by std::chrono



  • Hi !
    I have created an imagen to be displayed as QSplashScreen, the idea is show this image, wait 5 seconds and close it..

    Method 0:

    QSplashScreen _qs();
    _qs.setPixmap(QPixmap(":imgs/res/iss.png"));
    _qs.show();
    
    //wait 5 seconds
    const std::chrono::steady_clock::time_point ini = std::chrono::steady_clock::now();
    unsigned short t {0};
    
    while (t != 5){
         auto actual = std::chrono::steady_clock::now();
         t = std::chrono::duration_cast<std::chrono::seconds>(actual - ini).count();
    }
    _qs.close();
    

    Problem is that auto actual variable is freezing the _qs.show() call and I see no image on the screen... Maybe std::chrono uses threads somehow and current thread is sleeping.

    If i comment the while loop, then splash shows successfuly (i need to comment _qs.close() too to appreciate it..

    Method 1:

    _qs.show();
    int ini = QTime::currentTime().second(); //current second
    int fin = ini + 5;  //5 seconds limit
    int t {0}; //second to elapse
    
    do{
        t = QTime::currentTime().second(); //seconds elapsed
    } while (t < fin);
    
    _qs.close();
    

    There is a method 2 using a second thread and sleep this second thread, but same happens. Is there any way to fix it.. or use another way to acchieve?...

    Thanks.



  • yes...you are doing a while loop until t==5 in the main event loop thread, so no events are being processed during that interval. The incorrect "fix" is to call the event processor method inside the loop. The correct fix is to show() the window, then use a QTimer as a countdown and hide() the window on timer expiration.



  • @Kent-Dorfman

    Yes that has sense.. just solved it with QTimer::singleShot :

    QTimer::singleShot(5000, nullptr, [&](){ _qs.close(); w.show(); });
    

    Thanks for reply..



  • if solved then please mark as solved. Thanks.

    Another option is to subclass QDialog as a splash screen and exec_() the dialog.


Log in to reply