The startup screen is normal when the program is opened.
The qsplashscreen class is provided for this QT, but in the process of using it, it is always a flash, not what we want. We use the boot screen. If there is no module detection, we just want it to display for a few seconds. The following is my solution. inherit from qsplashscreen and add a timer.
# Ifndef splashscreen_h
#define SPLASHSCREEN_H
#include <QSplashScreen>
#include <QTimer>
class splashScreen : public QSplashScreen
{
Q_OBJECT
public:
explicit splashScreen(const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0);
// splashScreen(QWidget * parent, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0);
Void setshowsecond (INT _ second); // sets the display duration of the startup screen, in seconds, not microseconds.
Void starttimer () {Show (); timer. Start () ;}// the timer starts to work.
signals:
Void timeover (); // this signal is sent when the specified display duration is reached
public slots:
protected slots:
Void stoptimer (); // disable the timer
private:
~splashScreen(){}
Qtimer timer; // display duration Timer
};
#endif // SPLASHSCREEN_H
# Include "splashscreen. H"splashScreen::splashScreen(const QPixmap &pixmap, Qt::WindowFlags f) :
QSplashScreen(pixmap,f)
{}
//splashScreen::splashScreen(QWidget * parent, const QPixmap & pixmap = QPixmap(), Qt::WindowFlags f = 0):
// QSplashScreen(parent, pixmap, f)
//{//}
void splashScreen::setShowSecond(int _second)
{ timer.setInterval(_second*1000);
connect(&timer,SIGNAL(timeout()),this,SLOT(stopTimer()));
}
void splashScreen::stopTimer()
{ timer.stop();
emit timeOver();
close();
Deletelater (); // added here. It can only be used once and can only be used in heap.
}
Int main (INT argc, char * argv []){ QApplication a(argc, argv);
QPixmap pix(":/picture/splashScreen.jpg"); splashScreen *ps = new splashScreen(pix);
ps->setShowSecond(3);
ps->startTimer();
MainWindow w;
QObject::connect(ps,SIGNAL(timeOver()),&w,SLOT(show()));
W. Resize (qapplication: desktop ()-> size () * 0.9); // determine the size of the main interface based on the size of the display device.
return a.exec();
}
Why do I want to release it directly after the startup screen ends? It is useful at this time. I don't want to release it at the end of the program running. Resources are precious. In addition, the default mouse click operation is hide (). If you do not want to accidentally click the mouse and the screen disappears and the main interface is not displayed, rewrite void mousepressevent (qmousepressevent *) and leave the function body empty.