头文件
#ifndef SHUTDOWN_H
#define SHUTDOWN_H
#include <QDialog>
#include <QTimer>
namespace Ui {
class ShutDown;
}
class ShutDown : public QDialog
{
Q_OBJECT
public:
explicit ShutDown(QWidget *parent = 0);
~ShutDown();
private:
Ui::ShutDown *ui;
private slots:
void currTime();
void remainTime();
void on_btnOk_clicked();
void on_btnCancel_clicked();
private:
QTimer m_CurrTime;
QTimer m_remainTime;
QString m_time;
int time;
int time2;
};
#endif // SHUTDOWN_H
.cpp文件
#include "ShutDown.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ShutDown w;
w.show();
return a.exec();
}
#include "ShutDown.h"
#include "ui_ShutDown.h"
#include <QTime>
#include <QDebug>
#include <QFont>
ShutDown::ShutDown(QWidget *parent) :
QDialog(parent),
ui(new Ui::ShutDown)
{
ui->setupUi(this);
this->setWindowTitle("定时关机");
m_CurrTime.setInterval(1000);
connect(&m_CurrTime, SIGNAL(timeout()), this, SLOT(currTime()));
m_CurrTime.start();
m_remainTime.setInterval(1000);
connect(&m_remainTime, SIGNAL(timeout()), this, SLOT(remainTime()));
//m_remainTime.start();
//设置字体居中
ui->edtShutdownTime->setAlignment(Qt::AlignCenter);
//ui->edtCurrTime->setAlignment(Qt::AlignCenter);
ui->lblDisplayTime->setAlignment(Qt::AlignCenter);
}
ShutDown::~ShutDown()
{
delete ui;
}
void ShutDown::remainTime()
{
time2= time%60;
if(time2>=0)
{
ui->lblDisplayTime->setText(QString::number(time/60)+":"+QString::number(time2));
time2--;
}
time--;
}
void ShutDown::currTime()
{
QTime v_time= QTime::currentTime();
ui->edtCurrTime->setText(v_time.toString("hh:mm:ss"));
}
void ShutDown::on_btnOk_clicked()
{
this->m_remainTime.start(); //开启倒计时定时器
m_time= ui->edtShutdownTime->text();
time= m_time.toInt()*60;
QString v_shutdownTime= ui->edtShutdownTime->text(); //获取设定关机时间
QString command= "shutdown -s -t"; //关机命令
int second= 60*v_shutdownTime.toInt();
command+= " "+QString::number(second);
QChar *v_char= command.data();
qDebug()<<v_char;
system(command.toLatin1());
}
void ShutDown::on_btnCancel_clicked()
{
this->m_remainTime.stop();
QString command= "shutdown -a"; //取消关机命令
QChar *v_char= command.data();
qDebug()<<v_char;
system(command.toLatin1());
}