QT ------计时器
CLOCK.H SOURCE CODE:
#ifndef CLOCK_H
#define CLOCK_H
#include <QMainWindow>
#include <QTimer>
#include <stdlib.h>
#include <QMessageBox>
#include <QDebug>
#include <QString>
#include <QObject>
#include <QTimerEvent>
namespace Ui {
class CLOCK;
}
class CLOCK : public QMainWindow
{
Q_OBJECT
public:
explicit CLOCK(QWidget *parent = 0);
~CLOCK();
private:
Ui::CLOCK *ui;
int secondnumber;
QTimer *m_timer=new QTimer(this);
int m_nTimerID;
private slots:
void ClcokShutdown();
void ShowSetTimer();
void timernumber();
};
#endif // CLOCK_H
CLOCK SOURCE CODE:
#include "clock.h"
#include "ui_clock.h"
CLOCK::CLOCK(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CLOCK)
{
ui->setupUi(this);
//system("shutdown -s -t 7200");
//QTimer *timer=new QTimer(this);//创建时钟
//timer->start(1000*1);
//QObject::connect(timer,SIGNAL(timeout()),this,SLOT(close()));//设置启动时钟函数
QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(ShowSetTimer()));//启动单击事件
}
CLOCK::~CLOCK()
{
delete ui;
}
void CLOCK::ClcokShutdown()
{
system("shutdown -s -t 0");
}
void CLOCK::timernumber()
{
if(this->secondnumber>=0)
{
QString Temp;
Temp=QString::number(this->secondnumber);
ui->label_3->setText(Temp+".秒");
this->secondnumber--;
}
else
{
QMessageBox::information(this,"system Message","计时完成!!");
m_timer->stop();//停止记数
}
}
void CLOCK::ShowSetTimer()
{
if(!ui->lineEdit->text().isEmpty())
{
secondnumber=ui->lineEdit->text().toInt();
m_nTimerID=secondnumber;
ui->lineEdit->setEnabled(false);
QObject::killTimer(m_nTimerID*1000);
QObject::connect(m_timer,SIGNAL(timeout()),this,SLOT(timernumber()));
m_timer->start(1000);
}
else
{
QMessageBox::information(this,"系统提示","请先设置关机时间!!");
}
}
MAIN.CPP SOURCE CODE:
#include "clock.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CLOCK w;
w.setMaximumSize(288,213);
w.setMinimumSize(288,213);
w.show();
return a.exec();
}