×
Namespaces

Variants
Actions

Archived:QTimer example

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 (using C++ for the Qt app UI) is deprecated.

This article demonstrates how to use QTimer to trigger events. The demonstration app has a a number of buttons: when the timer expires we call setStyleSheet() and setProperty() on the buttons to change their appearance.

Article Metadata
Code Example
Source file: Media:Window.zip
Compatibility
Platform(s):
Symbian
Article
Created: divanov (11 Jun 2010)
Last edited: hamishwillee (11 Oct 2012)

Buttons.png

Contents

Main Class Implementation

#include <QtGui>
#include "window.h"
 
int main (int argc, char **argv)
{
QApplication app(argc, argv);
 
Window win;
win.show();
 
return app.exec();
}


Window Class Definition

#ifndef WINDOW_H
#define WINDOW_H
 
#include <QtGui>
 
#define NUM_OF_BUTTONS 50
 
class Window : public QMainWindow
{
Q_OBJECT
 
public:
Window(QWidget *parent=0);
 
private slots:
void buttonClicked(bool checked = false);
void timerTimeout();
 
private:
QPushButton *buttons[NUM_OF_BUTTONS];
QTimer *timer;
int timerCount;
};
 
#endif //WINDOW_H

When user press the button, timer started and will emit timeout() signal every second until 10 events will be produced or another button will be pressed


Window Class Implementation

#include "window.h"
 
Window::Window(QWidget *parent)
: QMainWindow(parent), timerCount(0)
{
QGridLayout *layout = new QGridLayout;
for (int i = 0; i < 10; ++i)
for (int j = 0; j < 5; ++j) {
buttons[i * 5 + j] = new QPushButton("Button");
buttons[i * 5 + j]->setFocusPolicy(Qt::NoFocus);
layout->addWidget(buttons[i * 5 + j], i, j);
connect(buttons[i * 5 + j], SIGNAL(clicked(bool)), this, SLOT(buttonClicked(bool)));
}
 
QWidget *w = new QWidget;
w->setLayout(layout);
 
setCentralWidget(w);
 
timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));
}
 
void Window::buttonClicked(bool checked)
{
Q_UNUSED(checked);
 
timerCount = 10;
if (!timer->isActive())
timer->start();
}
 
void Window::timerTimeout()
{
--timerCount;
if (timerCount <= 0)
timer->stop();
 
int idx = (qrand() % 50);
if (!buttons[idx]->property("coloured").isValid()) {
buttons[idx]->setStyleSheet("background-color: rgb(0, 255, 0); color: rgb(255, 255, 255)");
buttons[idx]->setProperty("coloured", true);
} else {
buttons[idx]->setStyleSheet("");
buttons[idx]->setProperty("coloured", QVariant());
}
}

when the timer expires it emit timerTimeout() slot which changes the property of the button.

Source Code

The full source code presented in this article is available here File:Window.zip

This page was last modified on 11 October 2012, at 04:14.
177 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.

×