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, "取消关机", "已取消关机操作!");
}
相关文章:

Qt制作定时关机小程序
文章目录 完成效果图ui界面ui样图 main函数窗口文件头文件cpp文件 引言 一般定时关机采用命令行模式,还需要我们计算在多久后关机,我们可以做一个小程序来定时关机 完成效果图 ui界面 <?xml version"1.0" encoding"UTF-8"?>…...
LeetCode day30
LeetCode day30 害,昨天和今天在搞数据结构的报告,后面应该也会把哈夫曼的大作业写上来。 今天认识认识贪心算法。(。・∀・)ノ 2697. 字典序最小回文串 给你一个由 小写英文字母 组成的字符串 s ,…...

数据分析基础之《numpy(5)—合并与分割》
了解即可,用panads 一、作用 实现数据的切分和合并,将数据进行切分合并处理 二、合并 1、numpy.hstack 水平拼接 # hstack 水平拼接 a np.array((1,2,3)) b np.array((2,3,4)) np.hstack((a, b))a np.array([[1], [2], [3]]) b np.array([[2], […...
centos 安装 Miniconda
在 CentOS 上安装 Miniconda 的步骤通常包括下载 Miniconda 安装脚本、运行脚本以及配置环境。以下是详细步骤: 1. 下载 Miniconda 安装脚本 首先,您需要从 Miniconda 的官方网站下载适用于 Linux 的安装脚本。您可以使用 wget 命令在 CentOS 终端中直…...
第二百二十六回
文章目录 1. 概念介绍2. 具体细节2.1 发现服务2.2 发现特征值2.3 发送数据2.4 接收数据 3. 代码与效果3.13.2 运行效果 4. 经验总结 我们在上一章回中介绍了"连接蓝牙设备的细节"相关的内容,本章回中将介绍通过蓝牙发送数据的细节.闲话休提,让…...
ubuntu常用指令
Ubuntu是一个基于Linux的操作系统,它使用了大量的命令行指令。这些指令对于管理系统、处理文件、监控资源和执行各种任务都非常有用。以下是一些常用的Ubuntu命令: 系统管理 sudo:提供管理员权限执行命令(例如 sudo apt update&a…...

Quartz.NET 事件监听器
1、调度器监听器 调度器本身收到的一些事件通知,接口ISchedulerListener,如作业的添加、删除、停止、挂起等事件通知,调度器的启动、关闭、出错等事件通知,触发器的暂停、挂起等事件通知,接口部分定义如下:…...

2024-AI人工智能学习-安装了pip install pydot但是还是报错
2024-AI人工智能学习-安装了pip install pydot但是还是报错 出现这样子的错误: /usr/local/bin/python3.11 /Users/wangyang/PycharmProjects/studyPython/tf_model.py 2023-12-24 22:59:02.238366: I tensorflow/core/platform/cpu_feature_guard.cc:182] This …...

在使用mapstruct,想忽略掉List<DTO>字段里面的,`data` 字段的映射, 如何写ignore: 使用@IterableMapping
在使用mapstruct,想忽略掉List字段里面的,data 字段的映射, 如何写ignore 代码如下: public interface AssigmentFileMapper {AssigmentFileDTO assigmentFileToAssigmentFileDTO(AssigmentFile assigmentFile);AssigmentFile assigmentFileDTOToAssigmentFile(Assigment…...

ansible-playbook的Temlates模块 tags模块 Roles模块
Temlates模块 jinja模板架构,通过模板可以实现向模板文件传参(python转义)把占位符参数传到配置文件中去,生产一个目标文本文件,传递变量到需要的配置文件当中 (web开发) nginx.conf.j2 早文件当中配置的是占位符(声明…...

Canal使用详解
Canal介绍 Canal是阿里巴巴开发的MySQL binlog增量订阅&消费组件,Canal是基于MySQL二进制日志的高性能数据同步系统。在阿里巴巴集团中被广泛使用,以提供可靠的低延迟增量数据管道。Canal Server能够解析MySQL Binlog并订阅数据更改,而C…...

【经典LeetCode算法题目专栏分类】【第8期】滑动窗口:最小覆盖子串、字符串排列、找所有字母异位词、 最长无重复子串
《博主简介》 小伙伴们好,我是阿旭。专注于人工智能AI、python、计算机视觉相关分享研究。 ✌更多学习资源,可关注公-仲-hao:【阿旭算法与机器学习】,共同学习交流~ 👍感谢小伙伴们点赞、关注! 《------往期经典推荐--…...
C#和.Net常见问题记录
什么是.NET框架,.NET框架与C#(C Sharp)是什么关系? .NET框架是由Microsoft设计和维护的软件开发框架,.NET框架提供了C#(编程语言)开发的所有基础设施和支持。通过使用C#和.NET框架,开发者可以轻松地开发高质量、高效率的应…...
FAQ:Container Classes篇
1、Why should I use container classes rather than simple arrays?(为什么应该使用容器类而不是简单的数组?) In terms of time and space, a contiguous array of any kind is just about the optimal construct for accessing a sequen…...
每日一题(LeetCode)----栈和队列--滑动窗口最大值
每日一题(LeetCode)----栈和队列–滑动窗口最大值 1.题目(239. 滑动窗口最大值) 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 …...
13.bash shell中的if-then语句
文章目录 shell中的流控制if语句if语句if-then语句if-then-else 语句 test命令数值比较字符串比较文件比较case语句 欢迎访问个人网络日志🌹🌹知行空间🌹🌹 shell中的流控制if语句 简单的脚本可以只包含顺序执行的命令࿰…...
深入了解 Python 的 import 语句
在 Python 中,import 语句是一个关键的功能,用于在程序中引入模块和包。本文将深入讨论 import 语句的各种用法、注意事项以及一些高级技巧,以帮助你更好地理解和使用这一功能。 概念介绍 package 通常对应一个文件夹,下面可以有…...

接口测试 — 11.logging日志模块处理流程
1、概括理解 了解了四大组件的基本定义之后,我们通过图示的方式来理解下信息的传递过程: 也就是获取的日志信息,进入到Logger日志器中,传递给处理器确定要输出到哪里,然后进行过滤器筛选,通过后再按照定义…...

Hago 的 Spark on ACK 实践
作者:华相 Hago 于 2018 年 4 月上线,是欢聚集团旗下的一款多人互动社交明星产品。Hago 融合优质的匹配能力和多样化的垂类场景,提供互动游戏、多人语音、视频直播、 3D 虚拟形象互动等多种社交玩法,致力于为用户打造高效、多样、…...

mac传输文件到windows
前言 由于mac系统与windows系统文件格式不同,通过U盘进行文件拷贝时,导致无法拷贝。官方解决方案如下,但是描述的比较模糊。看我的操作步骤即可。 https://support.apple.com/zh-cn/guide/mac-help/mchlp1657/12.0/mac/12.6 前提条件 mac与…...

利用最小二乘法找圆心和半径
#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...

Chapter03-Authentication vulnerabilities
文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...
Ubuntu系统下交叉编译openssl
一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机:Ubuntu 20.04.6 LTSHost:ARM32位交叉编译器:arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...
GitHub 趋势日报 (2025年06月08日)
📊 由 TrendForge 系统生成 | 🌐 https://trendforge.devlive.org/ 🌐 本日报中的项目描述已自动翻译为中文 📈 今日获星趋势图 今日获星趋势图 884 cognee 566 dify 414 HumanSystemOptimization 414 omni-tools 321 note-gen …...

嵌入式学习笔记DAY33(网络编程——TCP)
一、网络架构 C/S (client/server 客户端/服务器):由客户端和服务器端两个部分组成。客户端通常是用户使用的应用程序,负责提供用户界面和交互逻辑 ,接收用户输入,向服务器发送请求,并展示服务…...

MySQL 知识小结(一)
一、my.cnf配置详解 我们知道安装MySQL有两种方式来安装咱们的MySQL数据库,分别是二进制安装编译数据库或者使用三方yum来进行安装,第三方yum的安装相对于二进制压缩包的安装更快捷,但是文件存放起来数据比较冗余,用二进制能够更好管理咱们M…...

(一)单例模式
一、前言 单例模式属于六大创建型模式,即在软件设计过程中,主要关注创建对象的结果,并不关心创建对象的过程及细节。创建型设计模式将类对象的实例化过程进行抽象化接口设计,从而隐藏了类对象的实例是如何被创建的,封装了软件系统使用的具体对象类型。 六大创建型模式包括…...
前端中slice和splic的区别
1. slice slice 用于从数组中提取一部分元素,返回一个新的数组。 特点: 不修改原数组:slice 不会改变原数组,而是返回一个新的数组。提取数组的部分:slice 会根据指定的开始索引和结束索引提取数组的一部分。不包含…...
13.10 LangGraph多轮对话系统实战:Ollama私有部署+情感识别优化全解析
LangGraph多轮对话系统实战:Ollama私有部署+情感识别优化全解析 LanguageMentor 对话式训练系统架构与实现 关键词:多轮对话系统设计、场景化提示工程、情感识别优化、LangGraph 状态管理、Ollama 私有化部署 1. 对话训练系统技术架构 采用四层架构实现高扩展性的对话训练…...

C# WPF 左右布局实现学习笔记(1)
开发流程视频: https://www.youtube.com/watch?vCkHyDYeImjY&ab_channelC%23DesignPro Git源码: GitHub - CSharpDesignPro/Page-Navigation-using-MVVM: WPF - Page Navigation using MVVM 1. 新建工程 新建WPF应用(.NET Framework) 2.…...