qt中使用启动画面

来源:互联网 发布:中国汽车 知乎 编辑:程序博客网 时间:2024/06/07 21:06

                    在程序打开的时候,启动画面是很正常的。

                    对于这个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 *),函数体为空就行。