Qt实现启动画面

本文介绍如何使用Qt的QSplashScreen类实现应用程序启动时的欢迎画面,并展示如何添加进度条来指示启动进程。通过具体代码示例展示了两种场景:简单的文字提示启动画面和带有动态更新进度条的启动界面。
摘要由CSDN通过智能技术生成

  Qt可以使用QSplashScreen类来实现启动画面:

#include <QApplication>
#include <QSplashScreen>
#include <QPixmap>
#include <mainwindow.h>
#include <QDebug>
#include <QDateTime>

int main ( int argc, char *argv[] ) {
    QApplication app ( argc, argv );
    QPixmap pixmap ( "screen.png" );
    QSplashScreen screen ( pixmap );
    screen.show();
    screen.showMessage ( "LOVE", Qt::AlignCenter, Qt::red );
    /*-----------------------------------------------------*/
    QDateTime n = QDateTime::currentDateTime();
    QDateTime now;

    do {
        now = QDateTime::currentDateTime();
        app.processEvents();
    } while ( n.secsTo ( now ) <= 5 ); /* 延时5秒 */
    /*----------------------------------------------------*/
    MainWindow window;
    window.show();
    screen.finish ( &window );
    return app.exec();
}

  如果需要实现带进度条的启动界面,需要实现如下代码:
  mysplashscreen.h如下:

#ifndef __MYSPLASHSCREEN_H
#define __MYSPLASHSCREEN_H
#include <QtGui>

class MySplashScreen: public QSplashScreen {
    Q_OBJECT
private:
    QProgressBar *ProgressBar;
public:
    MySplashScreen ( const QPixmap &pixmap );
    ~MySplashScreen();
    void setProgress ( int value );
    void show_started ( void );
private slots:
    void progressChanged ( int );
};

#endif // __MYSPLASHSCREEN_H

  mysplashscreen.cpp如下所示:

#include "mysplashscreen.h"
#include <QDateTime>

MySplashScreen::MySplashScreen ( const QPixmap &pixmap ) : QSplashScreen ( pixmap ) {
    ProgressBar = new QProgressBar ( this ); /* 父类为MySplashScreen */
    ProgressBar->setGeometry ( 0, pixmap.height() - 50, pixmap.width(), 30 );
    ProgressBar->setRange ( 0, 100 );
    ProgressBar->setValue ( 0 );
    /* 值改变时,立刻repaint */
    connect ( ProgressBar, SIGNAL ( valueChanged ( int ) ), this, SLOT ( progressChanged ( int ) ) );
    QFont font;
    font.setPointSize ( 32 );
    ProgressBar->setFont ( font ); /* 设置进度条里面的字体 */
}

MySplashScreen::~MySplashScreen() {
}

void MySplashScreen::setProgress ( int value ) {
    ProgressBar->setValue ( value );
}

void MySplashScreen::progressChanged ( int ) {
    repaint();
}

void MySplashScreen::show_started ( void ) {
    this->show(); /* 显示 */
    this->setProgress ( 30 ); /* 显示30% */
    /* 这里需要插入延时函数 */
    this->setProgress ( 60 );
    /* 这里需要插入延时函数 */
    this->setProgress ( 90 );
    /* 这里需要插入延时函数 */
    this->setProgress ( 100 );
}

  main.cpp如下:

#include "widget.h"
#include <QApplication>
#include <QSplashScreen>
#include <QPixmap>
#include "mysplashscreen.h"

int main ( int argc, char *argv[] ) {
    QApplication app ( argc, argv );
    MySplashScreen *splash = new MySplashScreen ( QPixmap ( "./image/miaojie.png" ) );
    splash->show_started();
    app.processEvents();
    Widget w;
    w.show();
    splash->finish ( &w );
    return app.exec();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值