timer 클래스를 이용해서 간단한 스톱워치 구현해봤다..^-^;;
시작버튼과 멈춤버튼 처음부터 시작되는 초기화버튼을 넣었다
1초마다 timeout()을 발생해서 변수의 값을 1씩 올려주고
1씩 올려줄때마다 repaint()함수를 호출해서
paintEvent(QPaintEvent *) 을 실행시켜줌으로써 시간을 표시해줬다
여기에 많은 문제점이 있다
일단 변수가 int형으로 만들어서..변수범위가 넘을경우..쓰레기값이 표시가 될것이다..-_-;;
변수는 바꾸면 되겠지만
담에 할일은 이걸 thread형식으로 여러개 돌려볼생각이다
열심히 하자 ^^ 공부해서 남주냐? ㅋㅋ
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#define MYWIDGET_H
#include <qwidget.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include <qdatetime.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include <qdatetime.h>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget();
private slots:
void timerEvent();
void timerStop();
void timerStart();
void timerInit();
private:
QPushButton* bt1;
QPushButton* bt2;
QPushButton* bt3;
QPushButton* bt4;
QTimer* tr1;
QTime* t1;
QTime* t2;
QTime* n;
protected:
int t_count;
void paintEvent(QPaintEvent *);
};
{
Q_OBJECT
public:
MyWidget();
private slots:
void timerEvent();
void timerStop();
void timerStart();
void timerInit();
private:
QPushButton* bt1;
QPushButton* bt2;
QPushButton* bt3;
QPushButton* bt4;
QTimer* tr1;
QTime* t1;
QTime* t2;
QTime* n;
protected:
int t_count;
void paintEvent(QPaintEvent *);
};
#endif
mywidget.cpp
#include "mywidget.h"
MyWidget::MyWidget()
{
resize(240,300);
setCaption("Parking Management");
{
resize(240,300);
setCaption("Parking Management");
t_count=0;
bt1=new QPushButton("Off",this);
bt1->setGeometry(5,5,50,120);
bt1->setGeometry(5,5,50,120);
bt2=new QPushButton("Stop",this);
bt2->setGeometry(65,5,50,50);
bt2->setGeometry(65,5,50,50);
bt3=new QPushButton("Start",this);
bt3->setGeometry(65,70,50,50);
bt3->setGeometry(65,70,50,50);
bt4=new QPushButton("Init",this);
bt4->setGeometry(135,5,50,50);
bt4->setGeometry(135,5,50,50);
tr1=new QTimer(this);
// tr1->start(1000,false);
connect(tr1,SIGNAL(timeout()),this,SLOT(timerEvent()));
connect(bt2,SIGNAL(clicked()),this,SLOT(timerStop()));
connect(bt3,SIGNAL(clicked()),this,SLOT(timerStart()));
connect(bt4,SIGNAL(clicked()),this,SLOT(timerInit()));
}
// tr1->start(1000,false);
connect(tr1,SIGNAL(timeout()),this,SLOT(timerEvent()));
connect(bt2,SIGNAL(clicked()),this,SLOT(timerStop()));
connect(bt3,SIGNAL(clicked()),this,SLOT(timerStart()));
connect(bt4,SIGNAL(clicked()),this,SLOT(timerInit()));
}
void MyWidget::timerEvent()
{
t_count++;
repaint();
}
{
t_count++;
repaint();
}
void MyWidget::timerStop()
{
tr1->stop();
}
{
tr1->stop();
}
void MyWidget::timerInit()
{
t_count=0;
bt1->setText("00:00:00");
}
{
t_count=0;
bt1->setText("00:00:00");
}
void MyWidget::timerStart()
{
tr1->start(1000,false);
}
{
tr1->start(1000,false);
}
void MyWidget::paintEvent(QPaintEvent *)
{
QTime t2(0,0,0);
QTime n;
n=t2.addSecs(t_count);
bt1->setText(n.toString());
}
{
QTime t2(0,0,0);
QTime n;
n=t2.addSecs(t_count);
bt1->setText(n.toString());
}