16,817
社区成员




#include <QApplication>
#include <QMainWindow>
#include "splash_screen.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// 设置splash图片并传入主进程指针,用于更新event loop
SplashScreen *splashWindow = new SplashScreen(&app, QPixmap("./foo.png"));
splashWindow->show();
app.processEvents();
// 每隔1秒钟更新一步
int waitSecond = 1;
// 设置10步
int waitStep = 10;
for(int i = 0; i < waitStep; i ++) {
splashWindow->setCurrentProgress(i*100/waitStep);
splashWindow->showMessage("step " + QString().number(i*100/waitStep),
Qt::AlignBottom);
sleep(waitSecond);
}
QMainWindow window;
window.show();
splashWindow->finish(&window);
return app.exec();
}
#ifndef _SPLASHSCREEN_H
#define _SPLASHSCREEN_H
#include <QSplashScreen>
#include <QProgressBar>
#include <QApplication>
class SplashScreen : public QSplashScreen
{
Q_OBJECT
public:
explicit SplashScreen(QApplication *app, const QPixmap &pix);
virtual ~SplashScreen();
void setCurrentProgress(int value);
public slots:
private slots:
void progressBarUpdated(int);
protected:
virtual void drawContents(QPainter *painter);
private:
QApplication *app;
QProgressBar *progressBar;
int currVal;
};
#endif // _SPLASHSCREEN_H
#include "splash_screen.h"
SplashScreen::SplashScreen(QApplication *app, const QPixmap &pix)
: QSplashScreen(pix) {
this->app = app;
progressBar = new QProgressBar;
progressBar->setAlignment(Qt::AlignHCenter);
progressBar->resize(640, 19);
progressBar->setRange(0, 100);
progressBar->setValue(0);
progressBar->setEnabled(true);
progressBar->setStyleSheet("QProgressBar:horizontal {border: 2px solid gray; border-radius: 3px; background: white;padding: 1px;} QProgressBar::chunk:horizontal {background: blue}");
connect(progressBar, SIGNAL(valueChanged(int)), this, SLOT(progressBarUpdated(int)));
}
SplashScreen::~SplashScreen()
{
}
void SplashScreen::setCurrentProgress(int value)
{
currVal = value;
progressBar->setValue(currVal);
}
void SplashScreen::progressBarUpdated(int)
{
this->repaint();
this->app->processEvents();
}
void SplashScreen::drawContents(QPainter *painter)
{
QSplashScreen::drawContents(painter);
progressBar->render(painter);
}