Qt 的启动界面上添加进度条


主要是基于qsplashscreen写一个扩展类,

不罗嗦,直接看源码,关键部位有注释。


扩展类头文件

#ifndef __MYSPLASHSCREEN_H
#define __MYSPLASHSCREEN_H
#include 

class MySplashScreen: public QSplashScreen
{
        Q_OBJECT

private:
        QProgressBar *ProgressBar;


public:
        MySplashScreen(const QPixmap& pixmap);
        ~MySplashScreen();

        void setProgress(int value); //外部改变进度

private slots:
    void progressChanged(int);


};

#endif // __MYSPLASHSCREEN_H


扩展类实现文件:

#include "mysplashscreen.h"

MySplashScreen::MySplashScreen(const QPixmap& pixmap) : QSplashScreen(pixmap)


    ProgressBar = new QProgressBar(this); // 父类为MySplashScreen
    ProgressBar->setGeometry(0,576-32,1024,32);
    ProgressBar->setRange(0, 100);
    ProgressBar->setValue(0);

    connect(ProgressBar, SIGNAL(valueChanged(int)), this, SLOT(progressChanged(int))); //值改变时,立刻repaint

    QFont font;
    font.setPointSize(32);
    ProgressBar->setFont(font); // 设置进度条里面的字体

}

MySplashScreen::~MySplashScreen()
{
}

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


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

主文件

int main(int argc, char *argv[])
{

    QApplication app(argc, argv);
    MySplashScreen *splash = new MySplashScreen(QPixmap(":/images/logo.png"));
    splash->show(); // 显示
    splash->setGeometry(128,72,1024,576);

    splash->setProgress(30);// 显示%30
    sleep(3);

    splash->setProgress(60);
    sleep(3);
    
     splash->setProgress(90);
     sleep(3);


    GMainWindow *mainwin = new GMainWindow();
    mainwin->show(); 

    splash->finish(g_pWebWindow); // 消失
    delete splash;

    return app.exec();
}




你可能感兴趣的:(Linux编程)