在程序打开的时候,启动画面是很正常的。ide
对于这个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 *),函数体为空就行。