Écran de lancement avec barre de progression
Bonjour,
J'essaye de réaliser un splashScreen affichant la progression d'une fonction d'initialisation.
J'ai fait un petit code pour illustrer ce que je veux faire.
Je comprendrais que le sleep bloque le programme mais on demande de redessiner la fenêtre entre temps.
Le contenu de la fonction load est juste pour l'exemple.
Merci d'avance pour votre aide.
main.cpp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include "mainwindow.h"
#include <QApplication>
#include "splashscreen.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SplashScreen *splash = new SplashScreen(&a);
MainWindow w(splash);
splash->show();
w.load();
splash->finish(&w);
w.show();
return a.exec();
} |
splashscreen.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H
#include <QSplashScreen>
#include <QApplication>
class SplashScreen : public QSplashScreen
{
Q_OBJECT
public:
explicit SplashScreen(QApplication *app, QWidget *parent = 0);
int m_progress;
QApplication *app;
public slots:
void setProgress(int value)
{
m_progress = value;
if (m_progress > 100)
m_progress = 100;
if (m_progress < 0)
m_progress = 0;
update();
}
protected:
void drawContents(QPainter *painter);
};
#endif // SPLASHSCREEN_H |
splashscreen.cpp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include "splashscreen.h"
#include <QStyleOptionProgressBar>
SplashScreen::SplashScreen(QApplication *aApp, QWidget *parent) :
QSplashScreen(parent), app(aApp), m_progress(0)
{
this->setPixmap(QPixmap("C:/Users/T0181902/Downloads/ToolKit/inProgress/SplashScreen/SplashScreen/thales.png"));
this->setCursor(Qt::BusyCursor);
}
void SplashScreen::drawContents(QPainter *painter)
{
QSplashScreen::drawContents(painter);
// Set style for progressbar...
QStyleOptionProgressBar pbstyle;
pbstyle.initFrom(this);
pbstyle.state = QStyle::State_Enabled;
pbstyle.textVisible = true;
pbstyle.text = "Chargement en cours";
pbstyle.minimum = 0;
pbstyle.maximum = 100;
pbstyle.progress = m_progress;
pbstyle.invertedAppearance = false;
pbstyle.rect = QRect(0, 265, 380, 19); // Where is it.
// Draw it...
style()->drawControl(QStyle::CE_ProgressBar, &pbstyle, painter, this);
} |
mainwindow.h :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "splashscreen.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(SplashScreen *sp, QWidget *parent = 0);
~MainWindow();
void load();
signals:
void loadingUpdate(int progress, QString msg);
private:
Ui::MainWindow *ui;
SplashScreen *_sp;
};
#endif // MAINWINDOW_H |
mainwindow.cpp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
MainWindow::MainWindow(SplashScreen *sp, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
_sp(sp)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::load()
{
_sp->setProgress(20);
QThread::sleep(2);
_sp->setProgress(40);
QThread::sleep(2);
_sp->setProgress(60);
QThread::sleep(2);
_sp->setProgress(80);
QThread::sleep(2);
_sp->setProgress(100);
} |