#include "mainwindow.h"#include "ui_mainwindow.h"#include<QtGui>Timer::Timer (QWidget* p) : QWidget(p){ plcd = new QLCDNumber; plcd->setSegmentStyle(QLCDNumber::Flat); plcd->setFrameStyle(0); plcd->setMinimumSize(150,70); isOn = false; QTimer* timer = new QTimer(this); connect (timer, SIGNAL(timeout()), this, SLOT(showTime())); timer->start(1000); lbl = new QLabel; lbl->setAlignment(Qt::AlignCenter);//выравнивание lbl->setMinimumSize(100, 25); TimeEdit = new QTimeEdit(); QPushButton* timer_button = new QPushButton("Set Timer"); QPushButton* stop_button = new QPushButton("STOP");// alarm_time.isNull(); connect (timer_button, SIGNAL(clicked()), this, SLOT(setTimer())); //Timer p_timer = new QTimer(this); connect (p_timer, SIGNAL(timeout()), this, SLOT(getTimer())); p_timer->start(1000); getTimer(); QTimeEdit *timeEdit = new QTimeEdit(p); timeEdit->setDisplayFormat("hh:mm"); // Layout setup QVBoxLayout* pvbx = new QVBoxLayout; pvbx->addWidget(plcd); pvbx->addWidget(lbl); pvbx->addWidget(TimeEdit); pvbx->addWidget(timer_button); pvbx->addWidget(stop_button); pvbx->setSizeConstraint(QLayout::SetFixedSize); setLayout(pvbx); setWindowTitle (tr("TIMER"));}//Current Timevoid Timer::showTime(){ QTime time = QTime::currentTime(); QString text = time.toString("hh:mm"); if ((time.second() % 2) == 0) text[2] = ' '; plcd->display(text);}void Timer::setTimer(){}void Timer::getTimer(){}
C++ (Qt)#include "widget.h"#include "ui_widget.h" #include <QTime>#include <QTimer> QTimer* myTimer = new QTimer(); int hour, min, sec; Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); ui->lcdNumber->setNumDigits(8); QObject::connect(ui->pushButton_1, SIGNAL(clicked()), this, SLOT(MyEventHandler1())); QObject::connect(myTimer, SIGNAL(timeout()), this, SLOT(MyEventHandler2())); } Widget::~Widget(){ delete ui;} // обработка нажатия кнопки Startvoid Widget::MyEventHandler1(){ hour = 1; min = 15; sec = 0; myTimer->start(1000); return;} // обработка сигналов таймераvoid Widget::MyEventHandler2(){ QString text = QTime(hour, min, sec).toString("hh:mm:ss"); ui->label->setText(text); ui->lcdNumber->display(text); if(hour == 0 && min == 0 && sec == 0) myTimer->stop(); sec--; if(sec < 0) {min--; sec = 59;} if(min < 0) {hour--; min = 59;} return;}
#include "mainwindow.h"#include "ui_mainwindow.h"int m,h,s,summ;QTimer*pqtimer=new QTimer();Mustang::Mustang(QWidget *pw):QWidget(pw){isOn=false; time=new QTimeEdit(); Starttimer=new QPushButton("START",this); connect(Starttimer,SIGNAL(clicked()),this,SLOT(setTime())); //обновляе таймер// соединяем его с функцией connect(pqtimer,SIGNAL(timeout()),this,SLOT(getTime));; box=new QVBoxLayout; box->addWidget(time); box->addWidget(Starttimer); setLayout(box); setWindowTitle (tr("TIMERR"));}void Mustang::setTime(){ isOn=true; qtime = time->time(); h=(qtime.hour())/**3600000*/; m=(qtime.minute())/**60000*/; s=(qtime.second())/**1000*/; pqtimer->start(1000);QMessageBox* message = new QMessageBox(QMessageBox::Information, "TIMER","Your time thas been set.",QMessageBox::Ok);message->show();message->setAttribute(Qt::WA_DeleteOnClose,true);}void Mustang::getTime(){if(h==0&&m==0&&s==0)pqtimer->stop(); QMessageBox* message = new QMessageBox (QMessageBox::Information, "TIMER","TIME HAS OVER",QMessageBox::Ok); message->show(); message->setAttribute(Qt::WA_DeleteOnClose,true);s--;if(s<0){m--;s=59;}if(m<0){h--;m=59;}}
C++ (Qt) message->show(); message->setAttribute(Qt::WA_DeleteOnClose,true);
C++ (Qt)s--;if(s<0){m--;s=59;}if(m<0){h--;m=59;}