Qt制作定时关机小程序

news/2024/7/20 2:07:10 标签: 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_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, "取消关机", "已取消关机操作!");
}


http://www.niftyadmin.cn/n/5280931.html

相关文章

Postman接口测试(超详细整理)

常用的接口测试工具主要有以下几种 Postman&#xff1a;简单方便的接口调试工具&#xff0c;便于分享和协作。具有接口调试&#xff0c;接口集管理&#xff0c;环境配置&#xff0c;参数化&#xff0c;断言&#xff0c;批量执行&#xff0c;录制接口&#xff0c;Mock Server, …

03-JVM对象创建与内存分配机制深度剖析

文章目录 对象的创建对象创建的主要流程一、类加载检查二、分配内存划分内存的方法解决并发问题的方法 三、初始化零值四、设置对象头五、执行<init>方法 对象半初始化对象大小与指针压缩什么是java对象的指针压缩&#xff1f;为什么要进行指针压缩&#xff1f; 对象内存…

CentOS 7的新特性

CentOS 7在发布时相较于CentOS 6引入了许多重要的变化和优化。以下是一些主要的改进和新特性&#xff1a; 系统初始化程序&#xff1a;CentOS 7使用了systemd作为其初始化系统&#xff0c;取代了之前版本的init系统。systemd提供了更快的启动时间和更好的管理服务。 内核更新&…

服务器量化训练操作说明

Freespace服务器预训练主要步骤&#xff1a; 首先登录堡垒机&#xff0c;命令如下&#xff1a; ssh xxxrelay.baidu-int.com &#xff08;xxx为个人邮箱前缀&#xff09; 密码为个人邮箱密码 登录工作机&#xff0c;命令如下&#xff1a; ssh l3yq01-gpu-255-122-22-00.e…

[JS设计模式]Flyweight Pattern

Flyweight pattern 享元模式是一种结构化的设计模式&#xff0c;主要用于产生大量类似对象而内存又有限的场景。享元模式能节省内存。 假设一个国际化特大城市SZ&#xff1b;它有5个区&#xff0c;分别为nanshan、futian、luohu、baoan、longgang&#xff1b;每个区都有多个图…

GBASE南大通用数据库提供的高可用负载均衡功能

GBASE南大通用GBase 8a ODBC 提供的高可用负载均衡功能是指&#xff0c;GBase 8a ODBC 会将客户 端请求的数据库集群连接平均分摊到集群所有可用的节点上。 GBASE南大通用数据库负载均衡的使用方法 GBASE南大通用GBase 8a ODBC 提供两种方式来使用高可用负载均衡。一种是配置数…

Canny函数opencv

Canny函数opencv 在OpenCV中使用Canny函数时&#xff0c;其中的两个参数是用于边缘检测的阈值。具体来说&#xff0c;Canny(imgBlur, imgCanny, 25, 75); 这行代码中的两个阈值有特定的含义&#xff1a; 低阈值 (Low Threshold): 第一个参数25是低阈值。这个阈值用来控制边缘检…

redis主从复制(在虚拟机centos的docker下)

1.安装docker Docker安装(CentOS)简单使用-CSDN博客 2.编辑3个redis配置 cd /etc mkdir redis-ms cd redis-ms/ vim redis6379.conf vim redis6380.conf vim redis6381.conf# master #端口号 port 6379#设置客户端连接后进行任何其他指定前需要使用的密码 requirepass 12345…