zoukankan      html  css  js  c++  java
  • QSplashScreen类实现Qt程序启动画面

     
    QSplashScreen类实现Qt程序启动画面
    收藏人:zwsj    
    2013-09-13 | 阅:569  转:6  
     |   来源
      |  分享 
     
     
     
      
     
     

    程序启动画面一般用于显示软件信息(名称、作者、版权等)以及减少程序加载过程中的枯燥感。

    在Qt中,可以通过QSplashScreen类来为应用程序添加一个启动画面,它会在应用程序的主窗口出现前显示一个图片,并且可以在图片上显示想要输出的信息。

    下面是一个简单的例子:

    [cpp] view plaincopy
     
    1. #include <QApplication>  
    2. #include <QTextEdit>  
    3. #include <QSplashScreen>  
    4. #include <QtTest>  
    5. int main(int argc, char *argv[])  
    6. {  
    7.     QApplication app(argc, argv);  
    8.     QSplashScreen *splash = new QSplashScreen;  
    9.     splash->setPixmap(QPixmap(":/images/splash.png"));  
    10.     splash->show();  
    11.     Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;  
    12.     splash->showMessage(QObject::tr("Setting up the main Window..."),  
    13.                         topRight,  
    14.                         Qt::red);  
    15.     QTest::qSleep(3000);  
    16.     QTextEdit *textEdit = new QTextEdit;  
    17.     splash->showMessage(QObject::tr("Loading modules..."),  
    18.                         topRight,  
    19.                         Qt::blue);  
    20.     QTest::qSleep(3000);  
    21.     textEdit->show();  
    22.     splash->finish(textEdit);  
    23.     delete splash;  
    24.     return app.exec();  
    25. }  
     

    注意1:

    启动画面图片是通过setPixmap()来指定的,在这里图片是一个资源,因此,需要把图片添加到资源文件(.qrc)中;否则,看不到启动画面。

    注意2:

    在例子程序中,使用了QTest::qSleep()函数,因此,需要包含头文件<QTest>,并在.pro文件中,加入

            CONFIG += qtestlib

    最终效果如下:

    代码片段(2)[全屏查看所有代码]

    1. [代码]cpp代码     

    01 #include <QtGui/QtGui>
    02 #include <QtGui/QPixmap>
    03 #include <QtGui/QSplashScreen>
    04 #include "ui_browser.h"
    05  
    06 int main(int argc, char **argv)
    07 {
    08   QApplication app(argc, argv);
    09  
    10   QPixmap pixmap("splash.png");
    11     QSplashScreen *splash = new QSplashScreen(pixmap);
    12     splash->show();
    13   
    14   QMainWindow *form = new QMainWindow;
    15   Ui::MainWindow ui;
    16   ui.setupUi(form);
    17   ui.textBrowser->setSource(QString("files:///C:/Qt/4.1.2/doc/html/index.html"));
    18   form->show();
    19  
    20   splash->finish(form);
    21     delete splash;
    22  
    23   return app.exec();
    24 }

    2. [代码]而采用计时器来控制显示时间的话,可用下面方法自己制作SplashWindow。     跳至 [1] [2] [全屏预览]

    01 #include <QtGui/QtGui>
    02 #include <QtGui/QDialog>
    03 #include <QtCore/QTimer>
    04 #include "ui_browser.h"
    05  
    06 int main(int argc, char **argv)
    07 {
    08   QApplication app(argc, argv);
    09  
    10   QDialog dialog;
    11  
    12   QMainWindow *form = new QMainWindow;
    13   Ui::MainWindow ui;
    14   ui.setupUi(form);
    15   ui.textBrowser->setSource(QString("files:///C:/Qt/4.1.2/doc/html/index.html"));
    16  
    17   QTimer timer;
    18   QObject::connect(&timer, SIGNAL(timeout()), form, SLOT(show()));
    19   QObject::connect(&timer, SIGNAL(timeout()), &dialog, SLOT(accept()));
    20   timer.start(10000);
    21   dialog.exec();
    22  
    23   return app.exec();
    24 }
     
    一个样例程序,往往有一个启动界面一个方面是显得你的程序不那么呆板,同时你的一些初始化过程也可以在这个过程中完成
    QT当中提供了:一个类来实现

    #include
    #include "sortdialog.h"
    #include //提供启动画面的类

    int main(int argc,char *argv[])
    {
        QApplication app(argc,argv);
        QSplashScreen *splash = new QSplashScreen;
        splash->setPixmap(QPixmap("shot.png")); //这里提供在启动时显示的画面
        splash->show();
        Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
        splash ->showMessage(QObject::tr("Setting up the main window....."),topRight,Qt::red);
      // while(1);
        for(int i=0;i<1000;i++)
        {
            splash->repaint();
        }//这里做一个等待,如果有设置程序可以写在这里
        SortDialog *dialog = new SortDialog;  //这里生成主程序
        splash -> showMessage(QObject::tr("Loading modules..."),topRight,Qt::red);
        //loadModules();
        for(int i=0;i<1000;i++)
        {
            splash->repaint();
        }
        splash ->showMessage(QObject::tr("Establishing connecting....."),topRight,Qt::red);
        //establishConnections();

        dialog ->setColumnRange('C','F');
        for(int i=0;i<1000;i++)
        {
            splash->repaint();
        }
        splash->finish(dialog);
        dialog -> show();

        delete splash;//删掉,回收内存
        return app.exec();
    }
     
     
  • 相关阅读:
    那些ubuntu创建用户踩过的坑
    Build tools
    version control(以git为例)讲解
    URI和URL的区别
    HTTP解析过程心得
    函数式编程(functional programming)
    cb45a_c++_STL_算法_删除_(3)_unique(唯一的意思)删除连续性的重复的数据
    cb44a_c++_STL_算法_删除_(2)remove_copy_remove_copy_if
    cb43a_c++_STL_算法_删除_(1)remove_remove_if
    cb42a_c++_STL_算法_替换_replace
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4115919.html
Copyright © 2011-2022 走看看