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与…...

【OSG学习笔记】Day 18: 碰撞检测与物理交互
物理引擎(Physics Engine) 物理引擎 是一种通过计算机模拟物理规律(如力学、碰撞、重力、流体动力学等)的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互,广泛应用于 游戏开发、动画制作、虚…...
Java如何权衡是使用无序的数组还是有序的数组
在 Java 中,选择有序数组还是无序数组取决于具体场景的性能需求与操作特点。以下是关键权衡因素及决策指南: ⚖️ 核心权衡维度 维度有序数组无序数组查询性能二分查找 O(log n) ✅线性扫描 O(n) ❌插入/删除需移位维护顺序 O(n) ❌直接操作尾部 O(1) ✅内存开销与无序数组相…...

安宝特方案丨XRSOP人员作业标准化管理平台:AR智慧点检验收套件
在选煤厂、化工厂、钢铁厂等过程生产型企业,其生产设备的运行效率和非计划停机对工业制造效益有较大影响。 随着企业自动化和智能化建设的推进,需提前预防假检、错检、漏检,推动智慧生产运维系统数据的流动和现场赋能应用。同时,…...

什么是库存周转?如何用进销存系统提高库存周转率?
你可能听说过这样一句话: “利润不是赚出来的,是管出来的。” 尤其是在制造业、批发零售、电商这类“货堆成山”的行业,很多企业看着销售不错,账上却没钱、利润也不见了,一翻库存才发现: 一堆卖不动的旧货…...
【JavaSE】绘图与事件入门学习笔记
-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角,以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向,距离坐标原点x个像素;第二个是y坐标,表示当前位置为垂直方向,距离坐标原点y个像素。 坐标体系-像素 …...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)
船舶制造装配管理现状:装配工作依赖人工经验,装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书,但在实际执行中,工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...
智能AI电话机器人系统的识别能力现状与发展水平
一、引言 随着人工智能技术的飞速发展,AI电话机器人系统已经从简单的自动应答工具演变为具备复杂交互能力的智能助手。这类系统结合了语音识别、自然语言处理、情感计算和机器学习等多项前沿技术,在客户服务、营销推广、信息查询等领域发挥着越来越重要…...

Mysql中select查询语句的执行过程
目录 1、介绍 1.1、组件介绍 1.2、Sql执行顺序 2、执行流程 2.1. 连接与认证 2.2. 查询缓存 2.3. 语法解析(Parser) 2.4、执行sql 1. 预处理(Preprocessor) 2. 查询优化器(Optimizer) 3. 执行器…...
【Go语言基础【13】】函数、闭包、方法
文章目录 零、概述一、函数基础1、函数基础概念2、参数传递机制3、返回值特性3.1. 多返回值3.2. 命名返回值3.3. 错误处理 二、函数类型与高阶函数1. 函数类型定义2. 高阶函数(函数作为参数、返回值) 三、匿名函数与闭包1. 匿名函数(Lambda函…...

并发编程 - go版
1.并发编程基础概念 进程和线程 A. 进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位。B. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。C.一个进程可以创建和撤销多个线程;同一个进程中…...