Qt实现启动画面

  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();
}
Qt是一个跨平台的C++应用程序开发框架,它提供了丰富的GUI和媒体功能,使开发者能够快速创建各种应用程序。在Qt中,我们可以使用动态进度条和启动画面来增强用户体验。 动态进度条是一个可以显示任务进度的控件,它与任务的完成情况同步更新。我们可以使用Qt中的QProgressBar类来创建和管理动态进度条。在启动画面中,我们可以使用Qt中的QSplashScreen类来显示一个带有应用程序标识和加载信息的启动画面。 要创建动态进度条和启动画面,我们可以按照以下步骤进行: 1. 创建一个QSplashScreen实例,并设置启动画面的背景图片和显示文本。 2. 在应用程序主窗口的构造函数中,创建一个QProgressBar实例,并设置其初始值和范围。 3. 在任务执行过程中,使用QTimer类来模拟任务的进度,通过更新QProgressBar的value属性来更新进度条的显示。 4. 在任务完成后,关闭启动画面并显示应用程序主窗口。 下面是一个使用动态进度条和启动画面的示例代码: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); QSplashScreen splash(QPixmap(":/splash_image.png")); QProgressBar progressBar; splash.showMessage("Loading...", Qt::AlignBottom|Qt::AlignHCenter, Qt::white); splash.show(); QTime waitTime = QTime::currentTime().addSecs(3); // 模拟加载过程 while (QTime::currentTime() < waitTime) { app.processEvents(); } progressBar.setRange(0, 100); progressBar.setValue(0); progressBar.setAlignment(Qt::AlignCenter); QMainWindow mainWindow; mainWindow.setCentralWidget(&progressBar); mainWindow.show(); QTimer timer; QObject::connect(&timer, &QTimer::timeout, [&progressBar]() mutable { int value = progressBar.value() + 10; if (value <= progressBar.maximum()) { progressBar.setValue(value); } }); timer.start(500); splash.finish(&mainWindow); return app.exec(); } ``` 以上代码中,我们使用了一张启动画面图片,并设置了加载信息。然后,程序会在启动画面显示3秒钟,模拟加载过程。加载完成后,我们创建了应用程序的主窗口,并将QProgressBar设置为其中央部件。通过QTimer来定时更新进度条的值,最后关闭启动画面并显示主窗口。 使用动态进度条和启动画面可以给用户一个友好的界面提示,提升用户体验。在实际的应用程序中,我们可以根据具体需求来设计和实现更丰富和复杂的动态进度条和启动画面
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值