Archived:Creating a stop watch with QWidgets
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
This code snippet demonstrates how to create a simple stop watch QWidget based application. It provides a start, stop and reset functionality.
Article Metadata
Tested with
Compatibility
Platform Security
Article
Contents |
Overview
Here a widget is created with QLCDNumber and a three buttons. As soon as user clicks on the start button it starts a timer which generates a signal timeout every second. When a stop button is clicked it simply stops the timer and clicking reset will reset and stop the timer.
While testing this on a real device it is required that screen light should remain on until the stopwatch is running. XQUtils, class of Mobile extension, is used for resetting the inactivity timer and hence maintaining the screen light on. The XQUtils::resetInactivityTimer() is used to reset the system inactivity timer to keeps the background lights of the device on and Connect the timer’s timeout() signal to XQUtils::resetInactivityTime().
Related Link
- Read more about How to use QLCDNumber in Qt
- Read more about: QTimer
- See Creating a Digital Clock QWidget
Various Function of QTime And QLCDNumber
- Sets the time to hour h, minute m, seconds s and milliseconds ms.
time = new QTime;
time->setHMS(0,0,0,0);
- Sets this time to the current time. This is practical for timing.
QTime t;
t.start();
some_lengthy_task();
qDebug("Time elapsed: %d ms", t.elapsed());
- This property holds the style of the LCDNumber.
num->setSegmentStyle(QLCDNumber::Filled);
- This property holds the current number of LCD digits displayed.
num->setNumDigits(4);
Source File
#include "stopwatch.h"
#include <QString>
#include "xqutils.h"
stopwatch::stopwatch(QWidget *parent)
: QWidget(parent)
{
num = new QLCDNumber(this);
num->setNumDigits(8);
time = new QTime(this);
time->setHMS(0,0,0,0);
timer = new QTimer(this);
XQUtils *utils = new XQUtils(this);
QTimer *timer11 = new QTimer(this);
connect(timer11, SIGNAL(timeout()), utils, SLOT(resetInactivityTime()));
timer11->start(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
i=0;
layout = new QVBoxLayout(this);
hlayout = new QHBoxLayout(this);
reset = new QPushButton("Reset",this);
start = new QPushButton("Start",this);
stop = new QPushButton("Stop",this);
connect(reset,SIGNAL(clicked()),this,SLOT(resetTime()));
connect(start,SIGNAL(clicked()),this,SLOT(startTime()));
connect(stop,SIGNAL(clicked()),this,SLOT(stopTime()));
QString text = time->toString("hh:mm:ss");
num->display(text);
num->setStyleSheet("* { background-color:rgb(199,147,88);color:rgb(255,255,255); padding: 7px}}");
num->setSegmentStyle(QLCDNumber::Filled);
setStyleSheet("* { background-color:rgb(236,219,187)}}");
hlayout->addWidget(start);
hlayout->addWidget(stop);
hlayout->addWidget(reset);
layout->addWidget(num);
layout->addLayout(hlayout);
setLayout(layout);
resize(300,150);
}
stopwatch::~stopwatch()
{
// No need to delete any object that has a parent which is properly deleted.
}
void stopwatch::resetTime()
{
time->setHMS(0,0,0);
QString text = time->toString("hh:mm:ss");
num->display(text);
i=0;
stop->setDisabled(1);
start->setEnabled(1);
stopTime();
}
void stopwatch::startTime()
{
//flag=0;
start->setDisabled(1);
stop->setEnabled(1);
reset->setEnabled(1);
timer->start(1000);
}
void stopwatch::stopTime()
{
stop->setDisabled(1);
start->setEnabled(1);
reset->setEnabled(1);
timer->stop();
//flag=1;
//showTime();
}
void stopwatch::showTime()
{QTime newtime;
//if(flag==1)
//i=i-1;
i=i+1;
newtime=time->addSecs(i);
QString text = newtime.toString("hh:mm:ss");
num->display(text);
}
Header File
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include <QtGui/QWidget>
//#include "ui_stopwatch.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLCDNumber>
#include <QTime>
#include <QTimer>
#include <QHBoxLayout>
class stopwatch : public QWidget
{
Q_OBJECT
public:
int i;
bool flag;
stopwatch(QWidget *parent = 0);
~stopwatch();
private slots:
void resetTime();
void stopTime();
void startTime();
void showTime();
private:
QTime *time;
QTimer *timer;
QTimer *timer11;
QVBoxLayout *layout;
QHBoxLayout *hlayout;
QPushButton *reset;
QPushButton *start;
QPushButton *stop;
QLCDNumber *num;
};
#endif // STOPWATCH_H






30 Sep
2009