在程式開啟的時候,啟動畫面是很正常的。
對於這個qt提供了QSplashScreen類,可是我在使用過程中,他總是一閃而過,不是我們想要的。我們使用啟動畫面,如果沒有模組檢測,那我們只是想它顯示幾秒鐘而已。下面是我的辦法,繼承QSplashScreen,在加個定時器就行了。
#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);//設定啟動畫面的顯現時間長度,單位是秒,不是微秒
void startTimer() {show();timer.start();}//定時器開始工作
signals:
void timeOver();//達到規定的顯示時間長度發出此訊號
public slots:
protected slots:
void stopTimer();//關閉定時器
private:
~splashScreen(){}
QTimer 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();//此處是特意添加,只能在一次使用,且只能是在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);//以裝置的顯示器件的大小來確定主介面的大小
return a.exec();
}
為什麼我想在啟動畫面結束之後之後直接釋放,因為它就是這時候有用,我不想在程式運行結束的時候在釋放,資源是寶貴的。還有就是預設的滑鼠點擊操作是hide()。如果你不想不小心點了滑鼠畫面消失,並且主介面還沒有出來,還是重寫void mousePressEvent(QMousePressEvent *),函數體為空白就行。