How to create a splash screen with an induced delay: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Cleanup)
(Fixed example making just brute-force blocking synchronous sleep)
 
Line 3: Line 3:
We have {{DocLink|QSplashScreen}} which is used to cover up the starting delay of the program. Some times the programs might be quick enough, so that the splash screen may not be visible. We may induce some delay to show the splash, as a decoration!
We have {{DocLink|QSplashScreen}} which is used to cover up the starting delay of the program. Some times the programs might be quick enough, so that the splash screen may not be visible. We may induce some delay to show the splash, as a decoration!


Here in this example, using {{DocLink|QThread}} a delay is induced.
Single shot timer {{DocLink|QTimer}} will do.


<code>
<code>
#include <QApplication>
#include <QApplication>
#include <QSplashScreen>
#include <QSplashScreen>
#include <qthread.h>
#include <QTimer>
#include "mainwindow.h"
#include "mainwindow.h"
class I : public QThread
{
public:
    static void sleep(unsigned long secs) { QThread::sleep(secs); }
};


int main(int argc, char *argv[])
int main(int argc, char *argv[])
Line 21: Line 15:
     QApplication app(argc, argv);
     QApplication app(argc, argv);
     QPixmap pixmap("splash.jpg");
     QPixmap pixmap("splash.jpg");
     QSplashScreen splash(pixmap);
     QSplashScreen splash(pixmap, Qt::WindowStaysOnTopHint);
     splash.show();
     splash.show();
    QTimer::singleShot(5000, &splash, &QWidget::close); // keep displayed for 5 seconds
     MainWindow mainWin;
     MainWindow mainWin;
    mainWin.setWindowTitle("Application");
    I::sleep(5); // splash is shown for 5 seconds
     mainWin.showMaximized();
     mainWin.showMaximized();
    splash.finish(&mainWin);
     return app.exec();
     return app.exec();
}
}
</code>
</code>

Latest revision as of 06:54, 21 June 2019

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

We have QSplashScreen which is used to cover up the starting delay of the program. Some times the programs might be quick enough, so that the splash screen may not be visible. We may induce some delay to show the splash, as a decoration!

Single shot timer QTimer will do.

#include <QApplication>
#include <QSplashScreen>
#include <QTimer>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPixmap pixmap("splash.jpg");
    QSplashScreen splash(pixmap, Qt::WindowStaysOnTopHint);
    splash.show();
    QTimer::singleShot(5000, &splash, &QWidget::close); // keep displayed for 5 seconds
    MainWindow mainWin;
    mainWin.showMaximized();
    return app.exec();
}