文章目錄
- 完成效果圖
- ui界面
- ui樣圖
- main函數
- 窗口文件
- 頭文件
- cpp文件
引言
一般定時關機采用命令行模式,還需要我們計算在多久后關機,我們可以做一個小程序來定時關機
完成效果圖
ui界面
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>330</width><height>240</height></rect></property><property name="minimumSize"><size><width>330</width><height>240</height></size></property><property name="maximumSize"><size><width>330</width><height>240</height></size></property><property name="font"><font><pointsize>10</pointsize></font></property><widget class="QWidget" name="centralwidget"><layout class="QGridLayout" name="gridLayout_2"><item row="3" column="1"><widget class="QWidget" name="widget" native="true"><layout class="QGridLayout" name="gridLayout"><item row="2" column="0"><layout class="QHBoxLayout" name="horizontalLayout_2"><item><widget class="QPushButton" name="shutdownButton"><property name="text"><string>關機</string></property></widget></item><item><widget class="QPushButton" name="cancelShutdownButton"><property name="text"><string>取消</string></property></widget></item></layout></item><item row="0" column="0"><layout class="QHBoxLayout" name="horizontalLayout"><item><widget class="QComboBox" name="hourComboBox"><property name="minimumSize"><size><width>62</width><height>22</height></size></property><property name="maximumSize"><size><width>62</width><height>22</height></size></property><property name="editable"><bool>false</bool></property></widget></item><item><widget class="QLabel" name="hourLabel"><property name="minimumSize"><size><width>0</width><height>0</height></size></property><property name="text"><string>時</string></property></widget></item><item><spacer name="horizontalSpacer_3"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item><widget class="QComboBox" name="minuteComboBox"><property name="minimumSize"><size><width>62</width><height>22</height></size></property><property name="maximumSize"><size><width>62</width><height>22</height></size></property><property name="editable"><bool>false</bool></property></widget></item><item><widget class="QLabel" name="minuteLabel"><property name="text"><string>分</string></property></widget></item></layout></item><item row="1" column="0"><spacer name="verticalSpacer_4"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>30</height></size></property></spacer></item></layout></widget></item><item row="3" column="0"><spacer name="horizontalSpacer"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item row="0" column="1"><spacer name="verticalSpacer_2"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>40</height></size></property></spacer></item><item row="4" column="1"><spacer name="verticalSpacer"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>40</height></size></property></spacer></item><item row="3" column="2"><spacer name="horizontalSpacer_2"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item row="1" column="1"><widget class="QLabel" name="label"><property name="minimumSize"><size><width>186</width><height>30</height></size></property><property name="maximumSize"><size><width>186</width><height>30</height></size></property><property name="text"><string> 設置關機時間</string></property></widget></item><item row="2" column="1"><spacer name="verticalSpacer_3"><property name="orientation"><enum>Qt::Vertical</enum></property><property name="sizeHint" stdset="0"><size><width>20</width><height>30</height></size></property></spacer></item></layout></widget></widget><resources/><connections/>
</ui>
ui樣圖
main函數
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
窗口文件
核心邏輯
采用信號和槽,完成事件鏈接
QProcess::startDetached("shutdown", QStringList() << "/s" << "/t" << QString::number(timeSeconds));
QProcess::execute("shutdown", QStringList() << "/a");
頭文件
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTimer>
#include <QDateTime>
#include <QProcess>
#include <QMessageBox>
#include <QString>
#include <QDebug>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = nullptr);~MainWindow();
private slots:void onShutdownButtonClicked();void onCancelShutdownButtonClicked();private:Ui::MainWindow *ui;QTimer *shutdownTimer;};#endif // MAINWINDOW_H
cpp文件
// mainwindow.cpp#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);// 獲取當前時間QTime currentTime = QTime::currentTime();// 設置小時下拉框ui->hourComboBox->setEditable(false);for (int i = 0; i < 24; ++i) {// 比較當前時間與選項時間if (currentTime.hour() <= i) {ui->hourComboBox->addItem(QString::number(i));}}// 選擇當前小時作為已選中項ui->hourComboBox->setCurrentIndex(ui->hourComboBox->findText(QString::number(currentTime.hour())));// 設置分鐘下拉框ui->minuteComboBox->setEditable(false);for (int i = 0; i < 60; ++i) {// 比較當前時間與選項時間if (currentTime.minute() <= i) {ui->minuteComboBox->addItem(QString::number(i));}}// 選擇當前分鐘作為已選中項ui->minuteComboBox->setCurrentIndex(ui->minuteComboBox->findText(QString::number(currentTime.minute())));// 連接按鈕點擊事件到槽函數connect(ui->shutdownButton, &QPushButton::clicked, this, &MainWindow::onShutdownButtonClicked);connect(ui->cancelShutdownButton, &QPushButton::clicked, this, &MainWindow::onCancelShutdownButtonClicked);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::onShutdownButtonClicked()
{// 獲取用戶選擇的小時和分鐘int selectedHour = ui->hourComboBox->currentText().toInt();int selectedMinute = ui->minuteComboBox->currentText().toInt();// 獲取當前時間QDateTime currentTime = QDateTime::currentDateTime();// 獲取用戶選擇的時間QDateTime shutdownTime = QDateTime(currentTime.date(), QTime(selectedHour, selectedMinute));// 計算時間差qint64 timeDifference = currentTime.msecsTo(shutdownTime);int timeSeconds=int(timeDifference/1000);// 設置定時器的超時時間//shutdownTimer->start(timeDifference);QProcess::startDetached("shutdown", QStringList() << "/s" << "/t" << QString::number(timeSeconds));// 提示用戶關機已設置QMessageBox::information(this, "關機設置", "關機已設置,將在選擇的時間執行!");
}void MainWindow::onCancelShutdownButtonClicked()
{// 取消關機QProcess::execute("shutdown", QStringList() << "/a");QMessageBox::information(this, "取消關機", "已取消關機操作!");
}