Below is the code for three files called main.cpp and next window.cpp and last window.h
This example is for Linux as I have not tried it on Windows yet.
Make sure Qt 4 is installed.
Put the three files into an empty directory.
From the same directory, using a terminal type qmake -project and press return.
Next type qmake filename.pro and press return where filename is the same name as the directory.
Next type make followed by return.
In the same folder as the directory click on the icon the the same filename as the directory.
This is main.cpp
#include <QApplication>
#include "window.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
Window window;
window.show();
return app.exec();
}
This is window.cpp
#include <QSpinBox>
#include <QPushButton>
#include <QLCDNumber>
#include <QSpinBox>
#include "window.h"
#include <QTimer>
Window::Window(QWidget *parent) :
QWidget(parent)
{
setWindowTitle("Timer");
setFixedSize(400,400);
lCDNumber1 = new QLCDNumber(this);
lCDNumber1->setGeometry(100, 100, 80, 30);
lCDNumber1-> setFixedSize(60,60);
lCDNumber2 = new QLCDNumber(this);
lCDNumber2->setGeometry(200, 100, 80, 30);
lCDNumber2-> setFixedSize(60,60);
lCDNumber3 = new QLCDNumber(this);
lCDNumber3->setGeometry(300, 100, 80, 30);
lCDNumber3-> setFixedSize(60,60);
spinbox1 = new QSpinBox(this);
spinbox1->setGeometry(100, 200, 80, 30);
spinbox1->setRange(0, 24);
spinbox2 = new QSpinBox(this);
spinbox2->setGeometry(200, 200, 80, 30);
spinbox2->setRange(0, 60);
spinbox3 = new QSpinBox(this);
spinbox3->setGeometry(300, 200, 80, 30);
spinbox3->setRange(0, 60);
button1 = new QPushButton("Start", this);
button1->setGeometry(250, 300, 80, 30);
button2 = new QPushButton("Load", this);
button2->setGeometry(100, 300, 80, 30);
updatetimer = new QTimer(this);
connect(button1, SIGNAL(clicked()), this, SLOT(button1_clicked()));
connect(button2, SIGNAL(clicked()), this, SLOT(button2_clicked()));
connect(updatetimer, SIGNAL(timeout()), this, SLOT(update()));
}
void Window::button1_clicked()
{
updatetimer->start(1000);
}
void Window::update()
{
scount--;
if(scount<0) {
scount = 59;
mcount--;
}
if(mcount<0) {
mcount = 59;
hcount--;
}
if(hcount<0)
hcount = 24;
lCDNumber1->display(hcount);
lCDNumber2->display(mcount);
lCDNumber3->display(scount);
}
void Window::button2_clicked()
{
hcount = spinbox1->value();
mcount = spinbox2->value();
scount = spinbox3->value();
lCDNumber1->display(hcount);
lCDNumber2->display(mcount);
lCDNumber3->display(scount);
}
This is window.h
#define WINDOW_H
#include <QWidget>
class QSpinBox;
class QLCDNumber;
class QPushButton;
class QTimer;
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
int scount, mcount, hcount;
QLCDNumber *lCDNumber1;
QLCDNumber *lCDNumber2;
QLCDNumber *lCDNumber3;
QSpinBox *spinbox1;
QSpinBox *spinbox2;
QSpinBox *spinbox3;
QTimer *updatetimer;
signals:
private slots:
void update();
void button1_clicked();
void button2_clicked();
private:
QPushButton *button1;
QPushButton *button2;
// QTimer *updatetimer;
};
#endif // WINDOW_H