Browse Source

Gestion de l'affichage par un seul QLabel contenant un QTime

François Drouhard 9 years ago
parent
commit
ba15cac8d1
3 changed files with 20 additions and 94 deletions
  1. 14 79
      Chronometre.cpp
  2. 5 14
      Chronometre.h
  3. 1 1
      chronoCheckMate.pro.user

+ 14 - 79
Chronometre.cpp

@@ -28,35 +28,23 @@
 #include <QHBoxLayout>
 #include <QDebug>
 #include <QFont>
+#include <QTime>
 
-Chronometre::Chronometre (const QString & nom , QWidget * parent = 0) : QGroupBox (nom , parent) {
+Chronometre::Chronometre (const QString & nom , QWidget * parent = 0) :
+    QGroupBox (nom , parent),
+    ms(0)
+{
     setFlat(false);
-    heure = 0;
-	minute = 0;
-	seconde = 0;
-	centieme = 0;
 
+    temps = new QTime (0,0,0,0);
     QFont font("Times New Roman" , 22 , 5);
 
-    ecranHeure = new QLabel ("00", this);
-    ecranMinute = new QLabel ("00" , this);
-    ecranSeconde = new QLabel ("00" , this);
-    ecranCentieme = new QLabel ("00" , this);
-    ecranHeure->setFont(font);
-    ecranMinute->setFont(font);
-    ecranSeconde->setFont(font);
-    ecranCentieme->setFont(font);
+    ecranTemps = new QLabel (temps->toString("hh:mm::ss:zzz"));
+    ecranTemps->setFont(font);
 
 	ecranLayout = new QHBoxLayout;
-    ecranLayout->addWidget(ecranHeure);
-    ecranLayout->addWidget(new QLabel(":"));
-    ecranLayout->addWidget(ecranMinute);
-    ecranLayout->addWidget(new QLabel(":"));
-    ecranLayout->addWidget(ecranSeconde);
-    ecranLayout->addWidget(new QLabel(":"));
-    ecranLayout->addWidget(ecranCentieme);
+    ecranLayout->addWidget(ecranTemps);
     ecranLayout->addSpacing(40);
-    //ecranLayout->addStretch(-1);
 
 	setLayout (ecranLayout);
 	
@@ -68,29 +56,7 @@ Chronometre::Chronometre (const QString & nom , QWidget * parent = 0) : QGroupBo
 }
 
 void Chronometre::refresh () {
-	if (centieme == 99) {
-		if (seconde == 59) {
-			if (minute == 59) {
-				heure += 1;
-				minute = 0;
-				seconde = 0;
-				centieme = 0;
-			}
-			else {
-				minute += 1;
-				seconde = 0;
-				centieme = 0;
-			}
-		}
-		else {
-			seconde += 1;
-			centieme = 0;
-		}
-	}
-	else {
-		centieme += 1;
-	}
-	
+    ms += 10;
 	affichage ();
 }
 
@@ -99,51 +65,20 @@ void Chronometre::start () {
 }
 
 void Chronometre::stop () {
-	timer->stop();
+    timer->stop();
 }
 
-int Chronometre::getHeure () {
-	return heure;
-}
-
-int Chronometre::getMinute () {
-	return minute;
-}
-
-int Chronometre::getSeconde () {
-	return seconde;
-}
-
-int Chronometre::getCentieme () {
-	return centieme;
-}
-
-QString Chronometre::qString (int nombre) {
-    QString qNombre;
-    qNombre = "<b>";
-    if (nombre < 10) {
-        qNombre.append("0");
-    }
-    qNombre.append(QString::number(nombre));
-    qNombre.append("</b>");
-    return qNombre;
-}
 
 void Chronometre::affichage () {
-    ecranHeure->setText(qString (heure));
-    ecranMinute->setText(qString(minute));
-    ecranSeconde->setText(qString(seconde));
-    ecranCentieme->setText(qString(centieme));
+    QString chaineTemps = temps->addMSecs(ms).toString("hh:mm:ss:zzz");
+    ecranTemps->setText(chaineTemps);
 }
 
 void Chronometre::reinit () {
 	if (timer->isActive()) {
 		timer->stop();
 	}
-	heure = 0;
-	minute = 0;
-	seconde = 0;
-	centieme = 0;
+    ms  = 0;
 	affichage();
 }
 

+ 5 - 14
Chronometre.h

@@ -29,6 +29,7 @@ class QLabel;
 class QHBoxLayout;
 class QTimer;
 class QString;
+class QTime;
 
 class Chronometre : public QGroupBox {
 	
@@ -36,31 +37,21 @@ class Chronometre : public QGroupBox {
 	
 	public:
 	Chronometre (const QString & nom , QWidget * parent);
-	int getHeure ();
-	int getMinute ();
-	int getSeconde ();
-	int getCentieme ();
 	void start ();
 	void stop ();
 	void affichage ();
 	void reinit ();
 	void basculer ();
-    QString qString (int number);
 	
 	private slots:
 	void refresh ();
 	
 	private:
-	int heure;
-	int minute;
-	int seconde;
-	int centieme;
+    int ms;
 	QTimer *timer;
-	QHBoxLayout *ecranLayout;
-    QLabel *ecranHeure;
-    QLabel *ecranMinute;
-    QLabel *ecranSeconde;
-    QLabel *ecranCentieme;
+    QTime  *temps;
+    QHBoxLayout *ecranLayout;
+    QLabel *ecranTemps;
 };
 
 #endif

+ 1 - 1
chronoCheckMate.pro.user

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE QtCreatorProject>
-<!-- Written by Qt Creator 2.4.1, 2015-07-20T13:40:27. -->
+<!-- Written by Qt Creator 2.4.1, 2015-07-25T20:02:14. -->
 <qtcreator>
  <data>
   <variable>ProjectExplorer.Project.ActiveTarget</variable>