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