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,一经查实,立即删除!

相关文章

Visual Studio 编译优化选项:Debug与Release、禁止优化与O1、O2、Ox优化

Debug与禁止优化 Debug模式是调试模式&#xff0c;会有很多冗余的调试代码&#xff0c;供开发者调试程序使用。 VS是默认使用Debug模式的&#xff0c;我使用的是VS 2017。 在Debug模式下&#xff0c;是默认开启禁止优化的&#xff0c;我们来查看一下 在左侧源文件的main.c处…

【数字逻辑入门】计算机如何存储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;先判断整…

【蓝桥杯】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

【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…

使用ISE一键生成bit文件

我们知道&#xff0c;这几个&#xff0c;在第一次做好源文件之后&#xff0c;需要一个个进行右键单击-->run&#xff0c;以发现错误。 但是之后的调试&#xff0c;只要一点点变化&#xff0c;哪怕是注释变化&#xff0c;都需要重新run3次&#xff0c;太麻烦了。 不过经过实…

【FPGA Verilog】实验二:key按键基础实验

只说一下经验和教训 1 必须按照设计流程走 不要因为实验简单&#xff0c;就直接进行综合&#xff0c;比如按照 设计编码RTL优化仿真综合管脚分配&#xff0c;实现下载 一定要按照这个步骤来。 2 必须先查看开发板说明文档 开始出了一个令人困惑的问题&#xff0c;后来发现…

【Java】字符串转换为数字:Integer的parseInt方法

Java官方文档[1]的解释 public static int parseInt​(String s) throws NumberFormatException Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus…

在win10上使用Vmware安装Mac OS

安装macOS 如何在Windows上VMware上安装macOS Catalina 10.15 做一些提示&#xff1a; 如果您在第一次启动mac的时候&#xff0c;在出现【语言选择】之前&#xff0c;出现了连接蓝牙内容。 您可以将教程中【修改为win10 x64】那一步跳过&#xff0c;请注意&#xff0c;如果您…

【Computer Organization】The Core Design Thinking of single cycle CPU

1 Overview This section introduces someting that maybe you need to know before learning. Note:This CPU is based on MIPS instruction set. 1.1 Tools LogisimCS 3410 ComponentsMARS MIPS simulatorWin10 or Mac OS 1.2 Courses 自己动手画CPU《计算机组织与结构实…

【汇编语言】王爽 - 内中断复习

0 前言 基于王爽《汇编语言》和Coursera的《计算机组成》课程。 1 中断分类 CPU在执行指令的过程中&#xff0c;产生了一个异常/中断&#xff0c;因为CPU只能同时执行一条指令&#xff0c;所以需要暂停该指令的执行&#xff0c;转而去处理异常/中断信息。 这个异常可以来…

【算法】蛮力法/穷举法/枚举法 的基本问题分析

炮兵问题的优化&#xff0c;设立逻辑数组 蛮力法设计思想 有策略地穷举 验证 制定穷举策略避免重复 简单来说&#xff0c;就是列举问题所有可能的解&#xff0c;然后去看看是否满足题目要求&#xff0c;是一种逆向解题方式。&#xff08;我也不知道答案是什么&#xff0c;…

【计算机网络实验·北航】实验一:网络实验入门(1)

1.3 远程在线环境使用 PCA、PCB、PCC和PCD&#xff1a;4台PC机S1、S2&#xff1a;2台交换机R1、R2&#xff1a;2台路由器中间的设备&#xff1a;组网连线器 远程组网连线&#xff1a; 使用PCA上的组网连线软件&#xff0c;配置组网连线器&#xff0c;实现组网连线。 PCA和PCB…