Qt制作定时关机小程序

这篇具有很好参考价值的文章主要介绍了Qt制作定时关机小程序。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

引言

一般定时关机采用命令行模式,还需要我们计算在多久后关机,我们可以做一个小程序来定时关机

Qt制作定时关机小程序,笔记,qt,小程序,开发语言

完成效果图

Qt制作定时关机小程序,笔记,qt,小程序,开发语言
Qt制作定时关机小程序,笔记,qt,小程序,开发语言
Qt制作定时关机小程序,笔记,qt,小程序,开发语言

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样图

Qt制作定时关机小程序,笔记,qt,小程序,开发语言

main函数

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

窗口文件

核心逻辑
采用信号和槽,完成事件链接文章来源地址https://www.toymoban.com/news/detail-772478.html

 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_OBJECT

public:
    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制作定时关机小程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Qt 制作小程序登录系统(超详细)

    在这里我使用的是 Qt4, 在 windows 平台上来实现的。 首先 简要介绍此 登陆系统的基本作用。 登录系统效果展示 下面这就是 登录系统的主界面了。主要通过账户,密码来登录系统app. 关于效果展示的各个功能,都会 详细为大家讲解。 由上图可以看到 该界面主要由 图像,按钮

    2023年04月25日
    浏览(15)
  • QT网络编程TCP/UDP开发流程 制作网络调试助手

    1、QT的网络编程: TCP和UDP TCP编程需要用到俩个类: QTcpServer 和 QTcpSocket QTcpSocket类 提供了一个TCP套接字 QTcpSocket是QAbstractSocket的一个子类,它允许您建立TCP连接和传输数据流 注意:TCP套接字不能在QIODevice::Unbuffered模式下打开。 QTcpServer类 提供一个基于tcp的服务器 2. 这个类可以接

    2023年04月08日
    浏览(30)
  • 04-4_Qt 5.9 C++开发指南_时间日期与定时器

    时间日期是经常遇到的数据类型,Qt 中时间日期类型的类如下。 QTime:时间数据类型,仅表示时间,如 15:23:13。 QDate:日期数据类型,仅表示日期,如2017-4-5. QDateTime:日期时间数据类型,表示日期和时间,如2017-03-23 08:12:43. Qt 中有专门用于日期、时间编辑和显示的 界面组件 ,介

    2024年02月14日
    浏览(38)
  • 【Qt 学习笔记】Qt 开发环境的搭建 | Qt 安装教程

    博客主页:Duck Bro 博客主页 系列专栏:Qt 专栏 关注博主,后期持续更新系列文章 如果有错误感谢请大家批评指出,及时修改 感谢大家点赞👍收藏⭐评论✍ 文章编号:Qt 学习笔记 / 02 1. 安装包下载(网盘链接) 链接:https://pan.baidu.com/s/1t3jbYcFYFuJIfe0hPmeDmQ?pwd=duck 提取码:du

    2024年04月26日
    浏览(50)
  • 用bat写一个定时关机程序

    用Windows自带的命令行工具shutdown.exe来编写批处理定时关机程序。下面是一个简单的bat脚本示例: 方法一 设置秒数关机 打开记事本复制下面信息进去-另存为.bat文件-编码改为ANSI(不然可能会出现乱码) @echo off echo 请输入关机时间(单位:秒): set /p time= echo 将在 %time% 分钟后

    2024年02月04日
    浏览(33)
  • QT支持多种开发语言

    QT主要是一个C++应用程序框架,但它也提供了对其他一些编程语言的官方或非官方支持。以下是QT支持的一些语言版本及其特点。北京木奇移动技术有限公司,专业的软件外包开发公司,欢迎交流合作。 1.Python (PyQt)  : PyQt是QT的官方Python绑定,允许Python开发者使用QT库来创建

    2024年04月29日
    浏览(36)
  • <QT开发> QT开发工具-之-QT应用程序打包

    <QT开发> QT开发工具-之-QT应用程序打包 笔者为什么会写这篇文章呢?这是因为,笔者使用windows QT开发了一个测试工具。目的是通过TCP/IP测试其它应用程序。首先这个QT程序是笔者自己开发的,所以笔者的电脑当然是可以运行这个QT程序的,但是想要这个QT程序在其它同事的

    2024年02月12日
    浏览(39)
  • 【QT】QCustomPlot开发笔记

    01、QCustomPlot简介 QCustomPlot 是一个用于科学绘图的 QT 第三方库,可以用于常见的二维图像绘制,比如函数曲线、参数方程曲线、热力图、柱状图、箱线图、极坐标绘图等。该库由德国人 Emanuel Eichhammer 开发,经历了多次版本迭代,支持跨平台,开源遵循 GNU GPL 协议,也可以联

    2024年02月12日
    浏览(30)
  • Qt开发学习笔记02

    重写 mousePressEvent 方法 要在Qt中实现关机功能,你需要使用操作系统提供的相关函数或命令来执行关机操作。由于不同操作系统之间的关机方式可能有所不同,下面我将为你提供针对不同操作系统的示例代码。 Windows系统 也可使用下面方法 macOS系统 请注意,macOS系统需要使用

    2024年02月04日
    浏览(31)
  • Qt开发笔记-----基础篇

    Qt是一个跨平台的 C++ 图形用户界面应用程序框架 Qt 为应用程序开发者提供建立艺术级图形界面所需的所有功能 Qt 是完全面向对象的,很容易扩展,并且允许真正的组件编程 (1)Qt 发展史 在讲解学习 Qt 的必要性之前, 先来了解下 Qt 的发展历史: 1991年,Qt 最早由 奇趣科技

    2024年02月05日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包