公司门户网站开发,最专业网站建设哪家好,仙桃市住房建设局网站,成都设计公司招聘文章目录 完成效果图ui界面ui样图 main函数窗口文件头文件cpp文件 引言 一般定时关机采用命令行模式#xff0c;还需要我们计算在多久后关机#xff0c;我们可以做一个小程序来定时关机 完成效果图 ui界面
?xml version1.0 encodingUTF-8?还需要我们计算在多久后关机我们可以做一个小程序来定时关机 完成效果图 ui界面
?xml version1.0 encodingUTF-8?
ui version4.0classMainWindow/classwidget classQMainWindow nameMainWindowproperty namegeometryrectx0/xy0/ywidth330/widthheight240/height/rect/propertyproperty nameminimumSizesizewidth330/widthheight240/height/size/propertyproperty namemaximumSizesizewidth330/widthheight240/height/size/propertyproperty namefontfontpointsize10/pointsize/font/propertywidget classQWidget namecentralwidgetlayout classQGridLayout namegridLayout_2item row3 column1widget classQWidget namewidget nativetruelayout classQGridLayout namegridLayoutitem row2 column0layout classQHBoxLayout namehorizontalLayout_2itemwidget classQPushButton nameshutdownButtonproperty nametextstring关机/string/property/widget/itemitemwidget classQPushButton namecancelShutdownButtonproperty nametextstring取消/string/property/widget/item/layout/itemitem row0 column0layout classQHBoxLayout namehorizontalLayoutitemwidget classQComboBox namehourComboBoxproperty nameminimumSizesizewidth62/widthheight22/height/size/propertyproperty namemaximumSizesizewidth62/widthheight22/height/size/propertyproperty nameeditableboolfalse/bool/property/widget/itemitemwidget classQLabel namehourLabelproperty nameminimumSizesizewidth0/widthheight0/height/size/propertyproperty nametextstring时/string/property/widget/itemitemspacer namehorizontalSpacer_3property nameorientationenumQt::Horizontal/enum/propertyproperty namesizeHint stdset0sizewidth40/widthheight20/height/size/property/spacer/itemitemwidget classQComboBox nameminuteComboBoxproperty nameminimumSizesizewidth62/widthheight22/height/size/propertyproperty namemaximumSizesizewidth62/widthheight22/height/size/propertyproperty nameeditableboolfalse/bool/property/widget/itemitemwidget classQLabel nameminuteLabelproperty nametextstring分/string/property/widget/item/layout/itemitem row1 column0spacer nameverticalSpacer_4property nameorientationenumQt::Vertical/enum/propertyproperty namesizeHint stdset0sizewidth20/widthheight30/height/size/property/spacer/item/layout/widget/itemitem row3 column0spacer namehorizontalSpacerproperty nameorientationenumQt::Horizontal/enum/propertyproperty namesizeHint stdset0sizewidth40/widthheight20/height/size/property/spacer/itemitem row0 column1spacer nameverticalSpacer_2property nameorientationenumQt::Vertical/enum/propertyproperty namesizeHint stdset0sizewidth20/widthheight40/height/size/property/spacer/itemitem row4 column1spacer nameverticalSpacerproperty nameorientationenumQt::Vertical/enum/propertyproperty namesizeHint stdset0sizewidth20/widthheight40/height/size/property/spacer/itemitem row3 column2spacer namehorizontalSpacer_2property nameorientationenumQt::Horizontal/enum/propertyproperty namesizeHint stdset0sizewidth40/widthheight20/height/size/property/spacer/itemitem row1 column1widget classQLabel namelabelproperty nameminimumSizesizewidth186/widthheight30/height/size/propertyproperty namemaximumSizesizewidth186/widthheight30/height/size/propertyproperty nametextstring 设置关机时间/string/property/widget/itemitem row2 column1spacer nameverticalSpacer_3property nameorientationenumQt::Vertical/enum/propertyproperty namesizeHint stdset0sizewidth20/widthheight30/height/size/property/spacer/item/layout/widget/widgetresources/connections/
/ui
ui样图 main函数
#include mainwindow.h#include QApplicationint 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 QDebugnamespace 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.hMainWindow::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 timeSecondsint(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, 取消关机, 已取消关机操作);
}