• Qt5启动画面


    在QT5中,对于程序的启动界面是通过QSplashScreen来实现的,他会在应用程序的主窗口显示前显示一个图片,在该图片上可以通过文字来显示想要输出的信息。

    QSplashScreen在QT中的API如下:

    class Q_WIDGETS_EXPORT QSplashScreen : public QWidget
    {
        Q_OBJECT
    public:
        explicit QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = 0);
        QSplashScreen(QWidget *parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = 0);
        virtual ~QSplashScreen();
    
        void setPixmap(const QPixmap &pixmap);
        const QPixmap pixmap() const;
        void finish(QWidget *w);
        void repaint();
        QString message() const;
    
    public Q_SLOTS:
        void showMessage(const QString &message, int alignment = Qt::AlignLeft,
                      const QColor &color = Qt::black);
        void clearMessage();
    
    Q_SIGNALS:
        void messageChanged(const QString &message);
    
    protected:
        bool event(QEvent *e);
        virtual void drawContents(QPainter *painter);
        void mousePressEvent(QMouseEvent *);
    
    private:
        Q_DISABLE_COPY(QSplashScreen)
        Q_DECLARE_PRIVATE(QSplashScreen)
    };
    

     其中最常用的的几个函数API如下:

          void showMessage(const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black);//设置图片信息,message为显示的文本,alignment为对齐方式,color为文本颜色

    void finish(QWidget *w);//启动画面结束

    void setPixmap(const QPixmap &pixmap); //设置启动画面的图片

    此时显示一个启动画面的demo如下所示:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QSplashScreen>
    #include <QPixmap>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QPixmap pixmap(":/images/move.png");
        QSplashScreen *SplashScreen = new QSplashScreen;
        SplashScreen->setPixmap(pixmap);//设置启动图片
        SplashScreen->show();//显示
    
        Qt::Alignment align = Qt::AlignTop | Qt::AlignRight;//右上
        SplashScreen->showMessage("test",align,Qt::red);//设置图片上文本信息
    
        MainWindow w;
        SplashScreen->finish(&w);
        w.show();
    
        delete SplashScreen; //回收内存
        return app.exec();
    }
    

         此时,运行程序你会发现的确出现了启动画面,但是这个画面一闪而过,并没有起到太大的作用,因此我们想到延时,使用延时就必须使用processEvents()来处理消息,对于时间上的延迟可以通过QDateTimeQElapsedTimer  来实现。

         1 我们可以通过qint64 secsTo(const QDateTime &) const 来实现时间差,通过时间差来设定延时

         2 我们也可以通过qint64 QElapsedTimer::elapsed() const 来获取QElapsedTimer对象最近一次开始后的时间差,单位为毫秒

       3 我们可以通过QDateTime addSecs(qint64 secs) const先增加一个延时的时间间隔,在两个时间相等的时候就启动主界面

    #include "mainwindow.h"
    #include <QApplication>
    #include <QSplashScreen>
    #include <QPixmap>
    #include <QDateTime>
    #include <QElapsedTimer>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QPixmap pixmap(":/images/move.gif");
        QSplashScreen *SplashScreen = new QSplashScreen;
        SplashScreen->setPixmap(pixmap);
        SplashScreen->show();
    
        Qt::Alignment align = Qt::AlignTop | Qt::AlignRight;
        SplashScreen->showMessage("test",align,Qt::red);
    
        //设置延时
        //第一种方式
        QDateTime curDateTime = QDateTime::currentDateTime();
        QDateTime nowDateTime;
        do
        {
            nowDateTime = QDateTime::currentDateTime();
            app.processEvents();
    
        }while(curDateTime.secsTo(nowDateTime) <= 5);
    
        //第二种方式
    //    int nWaitTime = 5000;
    //    QElapsedTimer timer;
    //    timer.start();
    //    while(timer.elapsed() < nWaitTime){
    //        app.processEvents();
    //    }
    
        //第三种方式
    //    QDateTime curDateTime = QDateTime::currentDateTime();
    //    QDateTime furDateTime = curDateTime.addSecs(5);
    //    while(curDateTime != furDateTime){
    //        curDateTime = QDateTime::currentDateTime();
    //        app.processEvents();
    //    }
    
        MainWindow w;
        SplashScreen->finish(&w);
        w.show();
    
        delete SplashScreen;
        return app.exec();
    }
    

     参考博客:http://blog.csdn.net/chenlong12580/article/details/23713025

  • 相关阅读:
    sqlserver2012附加数据库2005版本时出现的问题
    jQuery实现评论还剩多少个字
    在mvc3中经常使用身份验证实现
    Windows Phone学习(1):棋子未动,先观全局
    使用jquery.pagination.js实现无刷新分页
    Javascript和JQuery中常用的随机数产生函数(很好用)
    网页打印样式设置(页眉,页脚,页边距)
    rdlc到设置宽度后自动换行(转)
    VSS忘记Admin密码和修改默认登陆用户
    解决jquery 修改onclick事件后IE兼容模式下立刻执行的问题
  • 原文地址:https://www.cnblogs.com/tianzhang/p/4931148.html
Copyright © 2020-2023  润新知