主函数如下:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPixmap pixmap(":/images/splash.JPG");
QSplashScreen splash(pixmap);
QGridLayout *layout = new QGridLayout( &splash );
Qt::Alignment bottommiddle = Qt::AlignCenter | Qt::AlignBottom;
QProgressBar *progressBar= new QProgressBar;
layout->addWidget(progressBar,0,0,bottommiddle );
splash.show();
app.processEvents();
MainWindow *mainwindow = new MainWindow();
mainwindow->show();
splash.finish(mainwindow);
return app.exec();
}
然后在mainwindow.cpp里,mainwindow()构造函数里调用progressBar->setValue(i);不成功,说progressBar没有调用,我把QProgressBar *progressBar= new QProgressBar;放到主函数外面编程全局变量也不行,这是怎么回事?应该怎么办?
------解决方案--------------------
把splash窗口的指针传递给QProgressBar试试。
------解决方案--------------------
三个文件,蓝色的置顶有边框的进度条,左下角显示文字信息
// main.cc
- C/C++ code
#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();}