大家好,又见面了,我是你们的朋友全栈君。
编写了一个简单的定时关机程序,软件界面如下。顺便记录一下所用的控件的一些简单的使用方法。程序中用到的控件主要是
QLCDNumber(数码管控件)以及QSpinBox()(滚动框控件)。
通过system()调用系统的关机命令。需要包含头文件stdlib.h。此外涉及到数码管以及滚动框显示数字时,前缀“0”的显示和消隐的问题。以下是代码。
首先是main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//Qt文本的国际化显示
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB18030"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));
MainWindow w;
w.show();
return a.exec();
}
其次是mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QMessageBox>
#include <stdlib.h>
#include <QTimer>
#include <QString>
#include <QByteArray>
#include <QSpinBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void slot_pushbutton_exec();
void slot_pushbutton_cancel();
void slot_count_down();
void slot_set_hour();
void slot_set_minute();
void slot_set_second();
private:
Ui::MainWindow *ui;
QTimer timer;
void init_sys();
};
#endif // MAINWINDOW_H
最后是mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
init_sys();
QObject::connect(ui->pushButton_exec,SIGNAL(clicked()),this,SLOT(slot_pushbutton_exec()));
QObject::connect(ui->pushButton_cancel,SIGNAL(clicked()),this,SLOT(slot_pushbutton_cancel()));
QObject::connect(&timer,SIGNAL(timeout()),this,SLOT(slot_count_down()));
QObject::connect(ui->spinBox_hour,SIGNAL(valueChanged(int)),this,SLOT(slot_set_hour()));
QObject::connect(ui->spinBox_minute,SIGNAL(valueChanged(int)),this,SLOT(slot_set_minute()));
QObject::connect(ui->spinBox_second,SIGNAL(valueChanged(int)),this,SLOT(slot_set_second()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init_sys()
{
//初始化LCD显示器
//注:display是重载函数,当希望显示09,08,07……等数字形式时
//采用字符串形式,可以显示前缀“0”,数字型显示会自动忽略掉0
ui->lcdNumber_hour->display("88");
ui->lcdNumber_minute->display("88");
ui->lcdNumber_second->display("88");
//设置组合框文本内容的前缀
ui->spinBox_hour->setPrefix("0");
ui->spinBox_minute->setPrefix("0");
ui->spinBox_second->setPrefix("0");
//初始化组合框文本内容
ui->spinBox_hour->setValue(0);
ui->spinBox_minute->setValue(0);
ui->spinBox_second->setValue(0);
//停止定时器
timer.stop();
}
void MainWindow::slot_pushbutton_exec()
{
//以索引号模式判断用户做出的选择
int index_model = ui->comboBox_model->currentIndex();
int index_action = ui->comboBox_action->currentIndex();
//以当前选项文本内容判断用户做出的选择
// QString str_model = ui->comboBox_model->currentText();
// QString str_action = ui->comboBox_action->currentText();
// if(str_model = "设定时间")
if(0==index_model) //指定关机时间模式
{
QString second = ui->spinBox_second->text();
QString minute = ui->spinBox_minute->text();
QString hour = ui->spinBox_hour->text();
QString cmd = "at "+hour+":"+minute+":"+second+" ";
switch(index_action)
{
case 0: //关机指令
cmd = cmd + "shutdown -s";
break;
case 1: //注销指令
cmd = cmd + "shutdown -l";
break;
case 2: //重启指令
cmd = cmd + "shutdown -r";
break;
}
//将QString类对象转换为char*类型
char* ch;
QByteArray ba = cmd.toLatin1();
ch=ba.data();
system(ch); //执行shell命令
}
else if(1==index_model) //倒计时关机模式
{
int delay = ui->spinBox_hour->value()*3600 +
ui->spinBox_minute->value()*60 +
ui->spinBox_second->value();
QString cmd = "";
switch(index_action)
{
case 0: //关机指令
cmd = "shutdown -s -t "+QString::number(delay);
break;
case 1: //注销指令
cmd = "shutdown -l -t "+QString::number(delay);
break;
case 2: //重启指令
cmd = "shutdown -r -t "+QString::number(delay);
break;
}
char* ch;
QByteArray ba = cmd.toLatin1();
ch=ba.data();
system(ch);
//当显示的数字的位数为一位时,需要加上前缀“0”,以达到09,08……的效果
//当显示的数字为两位数时,则需要屏蔽掉前缀“0”。
//设置LCD显示小时
if(ui->spinBox_hour->value()<=9&&ui->spinBox_hour->value()>=0)
ui->lcdNumber_hour->display("0"+QString::number(ui->spinBox_hour->value()));
else
ui->lcdNumber_hour->display(QString::number(ui->spinBox_hour->value()));
//设置LCD显示分钟
if(ui->spinBox_minute->value()<=9&&ui->spinBox_minute->value()>=0)
ui->lcdNumber_minute->display("0"+QString::number(ui->spinBox_minute->value()));
else
ui->lcdNumber_minute->display(QString::number(ui->spinBox_minute->value()));
//设置LCD显示秒
if(ui->spinBox_second->value()<=9&&ui->spinBox_second->value()>=0)
ui->lcdNumber_second->display("0"+QString::number(ui->spinBox_second->value()));
else
ui->lcdNumber_second->display(QString::number(ui->spinBox_second->value()));
timer.start(1*1000);
}
}
void MainWindow::slot_pushbutton_cancel()
{
system("shutdown -a");
init_sys();
}
void MainWindow::slot_count_down()
{
int hour = ui->lcdNumber_hour->value();
int minute = ui->lcdNumber_minute->value();
int second = ui->lcdNumber_second->value();
second--;
if(second<0)
{
second = 59;
minute--;
if(minute<0)
{
minute = 59;
hour--;
if(hour<0)
{
hour = 0;
}
}
}
if(hour<=9&&hour>=0)
ui->lcdNumber_hour->display("0"+QString::number(hour));
else
ui->lcdNumber_hour->display(QString::number(hour));
if(minute<=9&&minute>=0)
ui->lcdNumber_minute->display("0"+QString::number(minute));
else
ui->lcdNumber_minute->display(QString::number(minute));
if(second<=9&&second>=0)
ui->lcdNumber_second->display("0"+QString::number(second));
else
ui->lcdNumber_second->display(QString::number(second));
}
void MainWindow::slot_set_hour()
{
if(ui->spinBox_hour->value()>9)
{
ui->spinBox_hour->setPrefix("");
}
else
{
ui->spinBox_hour->setPrefix("0");
}
}
void MainWindow::slot_set_minute()
{
if(ui->spinBox_minute->value()>9)
{
ui->spinBox_minute->setPrefix("");
}
else
{
ui->spinBox_minute->setPrefix("0");
}
}
void MainWindow::slot_set_second()
{
if(ui->spinBox_second->value()>9)
{
ui->spinBox_second->setPrefix("");
}
else
{
ui->spinBox_second->setPrefix("0");
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/162985.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...