You Are Here:

Community: Wiki

This page was last modified on 30 October 2009, at 16:08.

Creating a Stop Watch with Qt for Symbian

From Forum Nokia Wiki

Reviewer Approved   



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


Overview

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.

Preconditions


Related Link

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

Screenshots

Image:stopwatch01.jpg

Image:stopwatch02.jpg

Image:stopwatch1.jpg

Image:stopwatch2.jpg

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fTalkE3aE4cargeE5fscreenE5fsaverX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ