17 Mart 2014 Pazartesi

Basic Qt Applications: QLCDNumber Application

Our first application was a basic QTimer application in which it is aimed to show the way of doing a certain task periodically by using a timer.

In this application, QTimer, QLCDNumber and QTime classes are used to implement a digital clock using an lcd digital format.

For this purpose, a display widget "LCD number" is inserted to the centralWidget as shown in figure.

By default Qt names the QLCDNumber object as "lcdNumber". Thus, we do not need to create a new
QLCDNumber object again.

Since we want to implement a digital clock we must include the <QTime> class and create a QTime object to access currentTime() function. The currentTime() function is used to get the current system time. However, in order to implement a digital clock we must access and display the current time periodically for every second. Thus, we will create a timer as in the QTimer application and call the myFunc() function as a slot to display the current time in the lcdNumber object.

mainwindow.h and mainwindow.cpp files are given below.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTime>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void myFunc();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
 
 
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QLCDNumber>
#include <QTime>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(myFunc()));
    timer->start(1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::myFunc()
{
    QTime t;
    ui->lcdNumber->setDigitCount(8);
    ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);

    ui->lcdNumber->setStyleSheet("color: green;"
                                 "background-color: white");

    ui->lcdNumber->display(t.currentTime().toString());

}

Some lcdNumber parameters such as text color and background color are set in myFunc() by using the following code pair. In order to view digital clock properly, lcdNumber digit count number must also be set to 8 for hh:mm:ss form.

    ui->lcdNumber->setDigitCount(8);
    ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
    ui->lcdNumber->setStyleSheet("color: green;"
                                 "background-color: white");

Program output can be seen from the figure below.

16 Mart 2014 Pazar

Basic Qt Applications: QTimer Application

This blog provides some application examples of Qt classes. As a first application "QTimer" class is used to show how to make a job peridocally by using a timer.

Examples are implemented in the Qt version of Qt 5.2.1 for Windows 32-bit (MinGW 4.8, OpenGL, 634 MB).  

QTimer Application

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void callTimer();
 
private slots:
    void myFunc();
 
private:
    Ui::MainWindow *ui;
};
 
#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    callTimer();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::callTimer(){
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(myFunc()));

    timer->start(1000);

}

void MainWindow::myFunc(){

    qDebug() << "Debugging is in progress...";
}



mainwindow.h and mainwindow.cpp files are respectively given above. Once a QTimer is created its timeout() signal should be connected to the appropriate slots. Slots can be considered as the functions that will be invoked after a timer call. Here, we define the myFunc() function as a slot which will be called periodically to perform some job. That's why in mainwindow.h file myFunc() is defined as a slot as shown above. 

In mainwindow.cpp file the callTimer() function is called in the constructor function of the MainWindow class. callTimer() function creates a timer object by using the following code pair.


QTimer *timer = new QTimer(this); 


To create a QTimer object in the mainwindow.cpp file the <QTimer> library must be included. Once a timer object created its timeout() signal is connected to the myFunc() function with the following code pair.
connect(timer, SIGNAL(timeout()), this, SLOT(myFunc()));

Here myFunc() can be any function that addresses your requirements. As an example, in this function a text is printed out by using another Qt class QDebug. Thus, it will be possible to observe if the timer is working properly or not. Please do not forget to add <QDebug> library to the mainwindow.cpp. Otherwise, qDebug() function will not be identified by the compiler.


timer is started by calling the start() function of QTimer class. The start() function asks the duration in milliseconds as a parameter from the user. Here, as it can be shown above, we passed start(1000) which makes 1 seconds. This means, the timer will call the myFunc() function periodically for every second.