| ID | ... | Creation date | 19 February 2009 |
| Platform | S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition | Tested on devices | N96 |
| Category | Qt for Symbian | Subcategory | Application |
| Keywords (APIs, classes, methods, functions): QLCDNumber , QTime, QTimer,XQutils |
This code snippet demonstrates how to create a simple stop watch in Qt for Symbian. It provides a start, stop and reset functionality.
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.
time = new QTime; time->setHMS(0,0,0,0);
QTime t;
t.start();
some_lengthy_task();
qDebug("Time elapsed: %d ms", t.elapsed());
num->setSegmentStyle(QLCDNumber::Filled);
num->setNumDigits(4);
#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);
}
#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
No related wiki articles found