最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Qt制作一個定時關機的小程序

 更新時間:2023年12月25日 11:32:19   作者:長安0511  
這篇文章主要為大家詳細介紹了如何基于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樣圖

main函數(shù)

#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())));

    // 連接按鈕點擊事件到槽函數(shù)
    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, "關機設置", "關機已設置,將在選擇的時間執(zhí)行!");
}

void MainWindow::onCancelShutdownButtonClicked()
{
    // 取消關機
    QProcess::execute("shutdown", QStringList() << "/a");
    QMessageBox::information(this, "取消關機", "已取消關機操作!");
}

以上就是基于Qt制作一個定時關機的小程序的詳細內(nèi)容,更多關于Qt定時關機的資料請關注腳本之家其它相關文章!

相關文章

  • C++針對bmp格式解析實例

    C++針對bmp格式解析實例

    這篇文章主要介紹了C++針對bmp格式解析實例,設計CWnd框架的使用及位圖的操作,需要的朋友可以參考下
    2014-10-10
  • C語言中函數(shù)返回字符串的方法匯總

    C語言中函數(shù)返回字符串的方法匯總

    C語言返回字符串函數(shù)共有四種方式,分別如下:使用堆空間,返回申請的堆地址,注意釋放、函數(shù)參數(shù)傳遞指針,返回該指針、返回函數(shù)內(nèi)定義的靜態(tài)變量(共享)、返回全局變量
    2017-05-05
  • C++引用及基本用法全解

    C++引用及基本用法全解

    本文解釋了C++中的引用概念,包括引用的三大特點(必須初始化、不能為空、不能改指向)、引用作為函數(shù)參數(shù)的三種方式、const引用的萬能特性、引用與指針的區(qū)別等,感興趣的朋友跟隨小編一起看看吧
    2026-04-04
  • CreateCompatibleDC()函數(shù)案例詳解

    CreateCompatibleDC()函數(shù)案例詳解

    這篇文章主要介紹了CreateCompatibleDC()函數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • c字符串,string對象,字符串字面值的區(qū)別詳解

    c字符串,string對象,字符串字面值的區(qū)別詳解

    以下是對c字符串,string對象,字符串字面值的區(qū)別進行了詳細的介紹,需要朋友可以 過來參考下
    2013-09-09
  • VS2019/2022配置C++ OpenCV4.10.0環(huán)境的圖文步驟

    VS2019/2022配置C++ OpenCV4.10.0環(huán)境的圖文步驟

    本文主要介紹了VS2019/2022配置C++ OpenCV4.10.0環(huán)境的圖文步驟,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2026-03-03
  • Opencv光流運動物體追蹤詳解

    Opencv光流運動物體追蹤詳解

    這篇文章主要為大家詳細介紹了Opencv光流運動物體追蹤的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 深入了解C++智能指針的使用

    深入了解C++智能指針的使用

    智能指針的本質(zhì)就是使用一個對象來接管一段開辟的空間,在該對象在銷毀的時候,自動調(diào)用析構(gòu)函數(shù)來釋放這段內(nèi)存。本文就來和大家詳細聊聊智能指針的使用,需要的可以參考一下
    2022-10-10
  • C++類的靜態(tài)成員初始化詳細講解

    C++類的靜態(tài)成員初始化詳細講解

    通常靜態(tài)數(shù)據(jù)成員在類聲明中聲明,在包含類方法的文件中初始化.初始化時使用作用域操作符來指出靜態(tài)成員所屬的類.但如果靜態(tài)成員是整型或是枚舉型const,則可以在類聲明中初始化
    2013-09-09
  • C++高性能服務器框架之線程模塊

    C++高性能服務器框架之線程模塊

    該模塊基于pthread實現(xiàn),sylar說,由于c++11中的thread也是由pthread封裝實現(xiàn)的,并且沒有提供讀寫互斥量,讀寫鎖,自旋鎖等,所以自己封裝了pthread,本文主要詳細介紹了C++高性能服務器框架中的線程模塊,需要的朋友可以參考下
    2023-06-06

最新評論

建阳市| 西乡县| 万源市| 扎兰屯市| 武安市| 泊头市| 甘泉县| 建湖县| 钟山县| 岑巩县| 陆丰市| 崇信县| 北安市| 通辽市| 霍邱县| 嘉荫县| 布拖县| 马山县| 英超| 沙洋县| 望江县| 镇巴县| 磐安县| 海兴县| 江口县| 鄂温| 成都市| 皮山县| 湘潭市| 若尔盖县| 呈贡县| 宁晋县| 大埔区| 恭城| 射洪县| 泽库县| 晋江市| 宜黄县| 尚志市| 新巴尔虎右旗| 黄冈市|