欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Qt制作一個(gè)定時(shí)關(guān)機(jī)的小程序

 更新時(shí)間:2023年12月25日 11:32:19   作者:長安0511  
這篇文章主要為大家詳細(xì)介紹了如何基于Qt制作一個(gè)有趣的定時(shí)關(guān)機(jī)的小程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

引言

一般定時(shí)關(guān)機(jī)采用命令行模式,還需要我們計(jì)算在多久后關(guān)機(jī),我們可以做一個(gè)小程序來定時(shí)關(guān)機(jī)

完成效果圖

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>關(guān)機(jī)</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>時(shí)</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>                   設(shè)置關(guān)機(jī)時(shí)間</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);

    // 獲取當(dāng)前時(shí)間
    QTime currentTime = QTime::currentTime();


    // 設(shè)置小時(shí)下拉框
    ui->hourComboBox->setEditable(false);
    for (int i = 0; i < 24; ++i) {
        // 比較當(dāng)前時(shí)間與選項(xiàng)時(shí)間
    if (currentTime.hour() <= i) {
            ui->hourComboBox->addItem(QString::number(i));
        }
    }
        // 選擇當(dāng)前小時(shí)作為已選中項(xiàng)
    ui->hourComboBox->setCurrentIndex(ui->hourComboBox->findText(QString::number(currentTime.hour())));

        // 設(shè)置分鐘下拉框
    ui->minuteComboBox->setEditable(false);
    for (int i = 0; i < 60; ++i) {
        // 比較當(dāng)前時(shí)間與選項(xiàng)時(shí)間
    if (currentTime.minute() <= i) {
            ui->minuteComboBox->addItem(QString::number(i));
        }
    }
        // 選擇當(dāng)前分鐘作為已選中項(xiàng)
    ui->minuteComboBox->setCurrentIndex(ui->minuteComboBox->findText(QString::number(currentTime.minute())));

    // 連接按鈕點(diǎn)擊事件到槽函數(shù)
    connect(ui->shutdownButton, &QPushButton::clicked, this, &MainWindow::onShutdownButtonClicked);
    connect(ui->cancelShutdownButton, &QPushButton::clicked, this, &MainWindow::onCancelShutdownButtonClicked);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onShutdownButtonClicked()
{
    // 獲取用戶選擇的小時(shí)和分鐘
    int selectedHour = ui->hourComboBox->currentText().toInt();
    int selectedMinute = ui->minuteComboBox->currentText().toInt();

    // 獲取當(dāng)前時(shí)間
    QDateTime currentTime = QDateTime::currentDateTime();

    // 獲取用戶選擇的時(shí)間
    QDateTime shutdownTime = QDateTime(currentTime.date(), QTime(selectedHour, selectedMinute));

    // 計(jì)算時(shí)間差
    qint64 timeDifference = currentTime.msecsTo(shutdownTime);

    int timeSeconds=int(timeDifference/1000);
    // 設(shè)置定時(shí)器的超時(shí)時(shí)間
    //shutdownTimer->start(timeDifference);

    QProcess::startDetached("shutdown", QStringList() << "/s" << "/t" << QString::number(timeSeconds));
    // 提示用戶關(guān)機(jī)已設(shè)置
    QMessageBox::information(this, "關(guān)機(jī)設(shè)置", "關(guān)機(jī)已設(shè)置,將在選擇的時(shí)間執(zhí)行!");
}

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

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

相關(guān)文章

  • c語言:基于函數(shù)指針的兩個(gè)示例分析

    c語言:基于函數(shù)指針的兩個(gè)示例分析

    本篇文章是對c語言中函數(shù)指針的兩個(gè)示例做了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 基于C語言實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)

    基于C語言實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)

    這篇文章主要介紹了基于C語言實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C語言超詳細(xì)講解順序表的各種操作

    C語言超詳細(xì)講解順序表的各種操作

    大家好,今天給大家?guī)淼氖琼樞虮恚矣X得順序表還是有比較難理解的地方的,于是我就把這一塊的內(nèi)容全部整理到了一起,希望能夠給剛剛進(jìn)行學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)的人帶來一些幫助,或者是已經(jīng)學(xué)過這塊的朋友們帶來更深的理解,我們現(xiàn)在就開始吧
    2022-05-05
  • Qt 智能指針的具體使用

    Qt 智能指針的具體使用

    本文主要介紹了Qt 智能指針的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • epoll封裝reactor原理剖析示例詳解

    epoll封裝reactor原理剖析示例詳解

    這篇文章主要為大家介紹了epoll封裝reactor原理剖析示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 一文詳解C++中的類型轉(zhuǎn)化

    一文詳解C++中的類型轉(zhuǎn)化

    說起類型轉(zhuǎn)化,我們在C語言之前的學(xué)習(xí)中可以了解到,類型轉(zhuǎn)換可以分為兩種情況:隱式類型轉(zhuǎn)化;顯示類型轉(zhuǎn)化。但是為什么在c++中還要繼續(xù)對類型轉(zhuǎn)化做文章呢?我們一起來看
    2023-04-04
  • C++中初始化二維數(shù)組的幾種常見方法

    C++中初始化二維數(shù)組的幾種常見方法

    本文詳細(xì)介紹了在C++中初始化二維數(shù)組的不同方式,包括靜態(tài)初始化、循環(huán)、全部為零、部分初始化、std::array和std::vector,以及std::vector作為動(dòng)態(tài)二維數(shù)組的靈活性和應(yīng)用場景,需要的朋友可以參考下
    2025-04-04
  • C++簡單又輕松的講解類和對象中友元函數(shù)

    C++簡單又輕松的講解類和對象中友元函數(shù)

    采用類的機(jī)制后實(shí)現(xiàn)了數(shù)據(jù)的隱藏與封裝,類的數(shù)據(jù)成員一般定義為私有成員,成員函數(shù)一般定義為公有的,依此提供類與外界間的通信接口。但是,有時(shí)需要定義一些函數(shù),這些函數(shù)不是類的一部分,但又需要頻繁地訪問類的數(shù)據(jù)成員,這時(shí)可以將這些函數(shù)定義為該類的友元函數(shù)
    2022-06-06
  • C++實(shí)現(xiàn)LeetCode(102.二叉樹層序遍歷)

    C++實(shí)現(xiàn)LeetCode(102.二叉樹層序遍歷)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(102.二叉樹層序遍歷),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++實(shí)現(xiàn)鏈表版本通訊錄

    C++實(shí)現(xiàn)鏈表版本通訊錄

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)鏈表版本通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12

最新評論