×
Namespaces

Variants
Actions
Revision as of 04:14, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:Creating a stop watch with QWidgets

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

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
Devices(s): N96
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Symbian
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QLCDNumber, QTime, XQutils
Created: james1980 (19 Feb 2009)
Last edited: hamishwillee (11 Oct 2012)


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

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

Stopwatch01.jpg

Stopwatch02.jpg

Stopwatch1.jpg

Stopwatch2.jpg

This page was last modified on 11 October 2012, at 04:14.
157 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×