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

Related Wiki Articles

No related wiki articles found

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: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fJ2ME45E5fE52SSE5fParserE5fwithE5fKE58mlX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxJ2ME45E20E52SSE20ParserE20withE20KE58mlE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZupdatedQDx2008E2d10E2d02X qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ