Qt制作定時關機小程序

文章目錄

  • 完成效果圖
  • 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, "取消關機", "已取消關機操作!");
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384717.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384717.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384717.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MIPS快速入門(原文+翻譯):MIPS Architecture and Assembly Language Overview(持續更新中)

前言 發布該文章的網站已經無法訪問&#xff0c;無法獲得相關翻譯授權&#xff0c;本人的翻譯僅供大家參考學習&#xff0c;盡可能使用直譯&#xff0c;并加上一些譯者注&#xff08;使用“ [1] ”的形式&#xff09;&#xff0c;以減少信息損失&#xff0c;水平有限&#xff…

Visual Studio 編譯優化選項:Debug與Release、禁止優化與O1、O2、Ox優化

Debug與禁止優化 Debug模式是調試模式&#xff0c;會有很多冗余的調試代碼&#xff0c;供開發者調試程序使用。 VS是默認使用Debug模式的&#xff0c;我使用的是VS 2017。 在Debug模式下&#xff0c;是默認開啟禁止優化的&#xff0c;我們來查看一下 在左側源文件的main.c處…

【匯編語言】記錄一組數中負數的個數,8086與MIPS匯編程序

題目及解答 統計由DATA開始的字節數據串中負元素的個數&#xff0c;數據個數在COUNT單元&#xff0c;統計結果存入RLT單元。 8086匯編&#xff1a; ; 統計數字中負數的個數【循環中加了個if else】 assume ds:datasg datasg segmentdata db 1,-2,-3,-1,-4,0,-2 count dw 7 ; 數…

【數字邏輯入門】計算機如何存儲1位二進制數

0 前言 本文將會以R-S鎖存器為例&#xff0c;引出鎖存器的核心和本質&#xff0c;之后再帶你構建更多類型的鎖存器&#xff0c;你能夠&#xff1a; 感受到由淺入深的學習方式體會到掌握核心本質的快感深刻理解核心套外殼的設計理念&#xff08;產品迭代1.0–>2.0–>3.0…

【算法訓練】DAY1:整數反轉

1 前言 題目來源于Leetcode。 重點&#xff1a;理清邏輯&#xff0c;忽略細節&#xff0c;模仿高手&#xff0c;五毒神掌 2 題目分析 題目很容易理解&#xff0c;先分成兩個部分 正數負數 先解決正數 最開始想到的是 intchar數組long唯一增加的就是&#xff0c;先判斷整…

【匯編語言】(x86)test與跳轉指令(je jle jge jg jl……)組合的含義

在x86指令集中&#xff0c;經常遇到test指令與條件跳轉指令組合&#xff0c;這是什么含義呢&#xff1f; 博主表示&#xff0c;查了很多資料也沒人完全說清楚…… 這里只用最簡單的&#xff0c;抽象層次進行說明&#xff0c;不講原理。 舉例 test edx,edx jle 某地址含義是&…

【藍橋杯】BASIC-8 回文數(2020-06-08)

題目 試題 基礎練習 回文數 資源限制 時間限制&#xff1a;1.0s 內存限制&#xff1a;512.0MB 問題描述   1221是一個非常特殊的數&#xff0c;它從左邊讀和從右邊讀是一樣的&#xff0c;編程求所有這樣的四位十進制數。    輸出格式   按從小到大的順序輸出滿足條件的…

【算法訓練】Leetcode 1295. 統計位數為偶數的數字(2020.06.09 )

1 題目 1295. 統計位數為偶數的數字 給你一個整數數組 nums&#xff0c;請你返回其中位數為 偶數 的數字的個數。 示例 1&#xff1a; 輸入&#xff1a;nums [12,345,2,6,7896] 輸出&#xff1a;2 解釋&#xff1a; 12 是 2 位數字&#xff08;位數為偶數&#xff09; 345 …

Vivado設置指定源文件進行RTL優化

像VS編譯器設置啟動項一樣&#xff0c;Vivado中&#xff0c;也有類似設計&#xff0c;可以看到&#xff0c;當前選中的是ALU&#xff0c;那么進行RTL優化的時候&#xff0c;會優化RTL的結果&#xff0c;而不是別的&#xff0c;如何改成別的&#xff1f; 在某文件上右鍵單擊選擇…

【完整流程】用VSCode替換Vivado默認編輯器

本文樓主找了很多資料&#xff0c;選出了最有用的資料&#xff0c;按照教程走&#xff0c;就可以順利搞定&#xff0c;先給出畫面 很酷很方便&#xff0c;同時還有 自動補全檢測錯誤列選自動生成仿真測試文件 等重要功能 Vivado原來的編輯器是這樣的…… 關鍵是&#xff0c…

IEDA中JavaDoc的自動生成、手動生成,以及生成html文檔

1 自動生成類的注釋 JavaDoc就是java特有的一種注釋。 1.1 配置 首先&#xff0c;IDEA點擊File-->Settings 然后Editor-->File and Code Templates-->Class 之后在這地方&#xff0c;添加一些代碼 /** * ${description} * * <p> * 創建日期&#xff1a;$…

【java】父類與子類的引用賦值關系

理清楚4個目標 父類引用&#xff08;“名”&#xff09;父類對象&#xff08;“實”&#xff09;子類引用子類對象 理清楚幾個操作 // 父類 public class parent{}// 子類 public class sun{}父類引用指向父類對象 parent p1 new parent();子類引用指向子類對象 son s1 …

IDEA自動生成 構造方法 get set方法

對于一個類&#xff0c;創建好成員變量后 右鍵單擊&#xff0c;選中Generate 然后 這幾個依次是 構造方法getsetget和set 我們可以選中一個&#xff0c;然后選中要生成的變量&#xff0c;點擊OK 這樣就可以自動生成 構成方法get方法set方法

IDEA快速修改類名和文件名

在你要修改的類名上&#xff0c;選中類名&#xff0c;然后 右鍵單擊選中Refactor選中Rename 也可以使用快捷鍵 Win用戶是Shift F6

java中 靜態方法與成員方法何時使用

靜態方法 不操作成員變量&#xff0c;可以直接調用 是用來直接對傳入的數據進行操作的 成員方法 需要操作對象的成員變量的 區別 靜態方法&#xff0c;不能操作成員變量&#xff0c;只是一個操作成員方法&#xff0c;可以操作成員變量&#xff0c;不僅僅是操作&#xff0…

通過編程解決問題的正確思路

1. 先知道我們面對一個怎樣的問題 2. 考慮這個問題在現實生活中&#xff0c;我們要用怎樣的方式去解決 3. 從現實到計算機&#xff0c;如何用編程的思路解決 4. 實現&#xff0c;編碼和測試 5. 迭代 現實問題自然語言解決方案機器語言解決方案編碼實現測試迭代

數據庫設計的核心原則 外鍵的設計 提高插入數據速度

大道至簡&#xff1a;數據庫設計的核心原則 數據庫設計&#xff0c;不得不承認&#xff0c;有很多專業化的理論知識&#xff0c;但是對于初學者來說&#xff0c;只需要大道至簡的原則就可以了。 能不重復的就不重復&#xff0c;太重復的就拆開&#xff0c;使用指定數據做識別…

MySQL提高插入數據的效率(結合JDBC)

0 解決問題最佳途徑&#xff1a;直接找官方 先說明的是&#xff0c;有問題直接去找官方文檔&#xff0c;而不應該去百度搜索&#xff0c;您很容易體驗到&#xff0c;搜索引擎很難快速找到真正對您有價值的解決方案&#xff0c;而官方文檔是最快捷的途徑。 本篇也是基于官方文…

【計算機心理學】先設計再實現 在實現中完善設計

先設計再實現 在物理學中&#xff0c;通常都是先理論證明觀點&#xff0c;再進行實踐&#xff0c;然后&#xff0c;再有世界各地的科學家根據理論進行實驗&#xff0c;以證明觀點正確。 在計算機軟件開發&#xff0c;硬件開發等&#xff0c;都講求先邏輯抽象設計&#xff0c;…

【FPGA VerilogHDL】第一次嘗試:LED燈基礎實驗

0 實驗環境 0.1 軟件環境 ISE 14.7win10vivado 2017.4 0.2 硬件設備 ISE適用的FPGA開發板&#xff1a;ALINK AX309 1 需求 能夠靈活控制4個LED燈 2 Verilog實現 timescale 1ns / 1ps // // Create Date: 14:18:20 08/08/2020 // Module Name: led // Revision…