QT5之定时关机

头文件

#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());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值