Animation : défilement de chiffres
Bonjour,
Voici ma contribution : un programme qui affiche un défilement de chiffres allant de 0 à 1000 ,dans une petite fenêtre de dimension (x =300,y = 70) pixels . Chaque image ,est séparé de 60 millisecondes d'interval .
Voici les deux fichiers du programme compilable :
chiffrdefil.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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #ifndef _CHIFFRDEFIL_H_
#define _CHIFFRDEFIL_H_
#include <QColor>
#include <QPainter>
#include <QPixmap>
#include <QString>
//#include <QTimer>
#include <QLabel>
#include <QString>
#include <QGridLayout>
//#include <QFont>
//#include <QSize>
#include <QWidget>
#include <QObject>
class Chiffredefile : public QObject
{ Q_OBJECT
public:
void Reset()
{ w_widget = new QWidget ;
layout = new QGridLayout ;
label = new QLabel ;
Chiffrevar = 0 ;
}
void EntierAjuste(int entier)
{ Chiffrevar = entier ; }
void widgetAjuste(QWidget * widget)
{ w_widget = widget ; }
public slots:
void EcrireSurImage()
{ QString phrase = "Defilement = " + QString::number(Chiffrevar) + " /1000" ;
QPixmap pixels(300,70) ;
pixels.fill(QColor::QColor(100,0,200)) ;
QPainter crayon(&pixels) ;
crayon.setPen( QColor::QColor(250,250,250)) ;
crayon.drawText(50,40,phrase) ;
crayon.end() ;
Chiffrevar++ ;
Image.operator=(pixels) ;
label->clear() ;
label->setPixmap(Image) ;
layout->addWidget(label) ;
emit AffichChange() ;
}
void AfficheDefil()
{ w_widget->setLayout(layout) ;
w_widget->update() ;
if (Chiffrevar == 1000)
{ emit fin() ; }
}
signals:
void AffichChange() ;
void fin() ;
private:
int Chiffrevar ; QPixmap Image ;
QGridLayout * layout ; QLabel * label ;
QWidget * w_widget ;
};
#endif |
chiffrdefil.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <QApplication>
#include <QTimer>
#include <QWidget>
#include "chiffrdefil.h"
#include <QObject>
int main(int argc ,char ** argv)
{ QApplication app(argc,argv) ;
Chiffredefile objet ;
objet.Reset() ;
objet.EntierAjuste(0) ;
QTimer * tempobj = new QTimer ;
QWidget * widget = new QWidget ;
widget->setFixedSize(300,80) ;
objet.widgetAjuste(widget) ;
widget->show() ;
QObject::connect(tempobj,SIGNAL(timeout()),&objet,SLOT(EcrireSurImage()) ) ;
QObject::connect(&objet,SIGNAL(AffichChange()),&objet,SLOT( AfficheDefil()) ) ;
QObject::connect(&objet,SIGNAL(fin()),&app,SLOT(quit()) );
tempobj->start(60) ;
app.exec() ;
return 0 ;
} |
Aurevoir...