Qt实现三种定时器的功能,来帮助我们定时去执行或者运行某个任务,采用独立的界面来实现,到点发送信号指示,动态更新倒计时器时间。
#ifndef TIMERTASKWIDGET_H
#define TIMERTASKWIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "timerhandler.h"
namespace Ui {
class TimerTaskWidget;
}
class TimerTaskWidget : public QWidget
{
Q_OBJECT
public:
explicit TimerTaskWidget(QWidget *parent = 0);
~TimerTaskWidget();
void init();
void getBeiJingTimer();
void stopTimer();
signals:
void sendTimerStop();//发送到线程
void sendTimerHandlerStart(int type, const QVariant& var); //发送线程开始计时
void sendTimerStart(); //发送到主界面
void sendTimeStr(QString str);
private slots:
void on_pushButton_Save_clicked();
void on_radioButton_DingShi_clicked();
void on_radioButton_DaoJiShi_clicked();
void onNetworkFinish();
void on_pushButton_clicked();
void on_comboBox_DingShi_TimeType_currentIndexChanged(int index);
void on_comboBox_JiHua_TimeType_currentIndexChanged(int index);
void on_radioButton_JiHua_clicked();
void wait(int ms); //等待
private:
Ui::TimerTaskWidget *ui;
QNetworkAccessManager networkManager;
QNetworkReply* reply;
TimerHandler* m_TimerHandler;
QDateTime m_BeiJingTime;
int m_IsGetSucceed; // -1开始等待 1获取成功 0获取失败 -2循环等待
};
#endif // TIMERTASKWIDGET_H
#include "timertaskwidget.h"
#include "ui_timertaskwidget.h"
#include
#include
#include
#include
#include
TimerTaskWidget::TimerTaskWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::TimerTaskWidget)
{
ui->setupUi(this);
this->setWindowTitle("定时器编辑");
setWindowFlags(this->windowFlags() & ~Qt::WindowMinimizeButtonHint); // 隐藏最小化按钮
ui->comboBox_JiHua_TimeType->setView(new QListView);
ui->comboBox_DingShi_TimeType->setView(new QListView);
ui->comboBox_DingShi->setView(new QListView);
m_TimerHandler = new TimerHandler();
connect(m_TimerHandler, SIGNAL(sendTimerStart()), this, SIGNAL(sendTimerStart()));
connect(m_TimerHandler, SIGNAL(sendTimeStr(QString)), this, SIGNAL(sendTimeStr(QString)));
connect(this, SIGNAL(sendTimerStop()), m_TimerHandler, SLOT(onReadStop()));
connect(this, SIGNAL(sendTimerHandlerStart(int,QVariant)), m_TimerHandler, SLOT(onTimerHandlerStart(int,QVariant)));
init();
}
TimerTaskWidget::~TimerTaskWidget()
{
delete ui;
}
void TimerTaskWidget::init()
{
ui->stackedWidget->setCurrentIndex(0);
ui->dateTimeEdit_DingShi->setDateTime(QDateTime::currentDateTime());
ui->dateTimeEdit_JiHua->setDateTime(QDateTime::currentDateTime());
ui->comboBox_DingShi->setCurrentIndex(0);
}
void TimerTaskWidget::getBeiJingTimer()
{
QNetworkRequest request(QUrl("http://worldtimeapi.org/api/timezone/Asia/Shanghai"));
reply = networkManager.get(request);
connect(reply, SIGNAL(finished()), this, SLOT(onNetworkFinish()));
m_IsGetSucceed = -1;
m_BeiJingTime = QDateTime();
}
void TimerTaskWidget::stopTimer()
{
emit sendTimerStop();
}
void TimerTaskWidget::on_pushButton_Save_clicked()
{
int type = 0;
QVariant var;
if (ui->radioButton_JiHua->isChecked()) {
type = 0;
JiHuaData jihua;
jihua.timeType = ui->comboBox_JiHua_TimeType->currentIndex();
jihua.time = ui->dateTimeEdit_JiHua->dateTime();
if (jihua.timeType == 0) {
if (jihua.time < QDateTime::currentDateTime()) {
QMessageBox::warning(this, "警告", "计划时间不可小于当前时间!");
return;
}
}
else if (jihua.timeType == 1) {
m_IsGetSucceed = -1;
getBeiJingTimer();
while (m_IsGetSucceed < 0) {
wait(10);
if (m_IsGetSucceed == 1) {
if (!m_BeiJingTime.isNull()) {
if (jihua.time < m_BeiJingTime) {
QMessageBox::warning(this, "警告", "计划时间不可小于当前时间!");
return;
}
}
}
else if (m_IsGetSucceed == 0) {
QMessageBox::warning(this, "警告", "北京时间获取失败,或者网络无连接!");
return;
}
}
}
var.setValue<JiHuaData>(jihua);
}
else if (ui->radioButton_DingShi->isChecked()) {
type = 1;
DingShiData dingshi;
dingshi.timeType = ui->comboBox_DingShi_TimeType->currentIndex();
dingshi.time = ui->dateTimeEdit_DingShi->dateTime();
dingshi.isRepeat = ui->comboBox_DingShi->currentIndex();
if (dingshi.timeType == 0) {
if (dingshi.time.time() < QTime::currentTime()) {
QMessageBox::warning(this, "警告", "定时时间不可小于当前时间!");
return;
}
}
else if (dingshi.timeType == 0) {
m_IsGetSucceed = -1;
getBeiJingTimer();
while (m_IsGetSucceed < 0) {
wait(10);
if (m_IsGetSucceed == 1) {
if (!m_BeiJingTime.isNull()) {
if (dingshi.time.time() < m_BeiJingTime.time()) {
QMessageBox::warning(this, "警告", "计划时间不可小于当前时间!");
return;
}
}
}
else if (m_IsGetSucceed == 0) {
QMessageBox::warning(this, "警告", "北京时间获取失败,或者网络无连接!");
return;
}
}
}
var.setValue<DingShiData>(dingshi);
}
else if (ui->radioButton_DaoJiShi->isChecked()) {
type = 2;
DaoJiShiData daojishi;
daojishi.day = ui->spinBox_Day->value();
daojishi.hour = ui->spinBox_Hour->value();
daojishi.minute = ui->spinBox_Minute->value();
daojishi.second = ui->spinBox_Second->value();
daojishi.daoJiShiNumber = ((daojishi.day * 24 * 60 * 60) + (daojishi.hour * 60 * 60) + (daojishi.minute * 60) + daojishi.second);
daojishi.daoJiShiNumber *= 1000;
daojishi.isRepeat = ui->checkBox_DaoJiShi->isChecked();
if (daojishi.daoJiShiNumber <= 0) {
QMessageBox::warning(this, "警告", "请设置倒计时时间!");
return;
}
var.setValue<DaoJiShiData>(daojishi);
}
emit sendTimerHandlerStart(type, var);
this->hide();
}
void TimerTaskWidget::on_radioButton_JiHua_clicked()
{
emit sendTimerStop();
if (ui->radioButton_JiHua->isChecked()) {
ui->stackedWidget->setCurrentIndex(0);
}
}
void TimerTaskWidget::on_radioButton_DingShi_clicked()
{
emit sendTimerStop();
if (ui->radioButton_DingShi->isChecked()) {
ui->stackedWidget->setCurrentIndex(1);
}
}
void TimerTaskWidget::on_radioButton_DaoJiShi_clicked()
{
emit sendTimerStop();
if (ui->radioButton_DaoJiShi->isChecked()) {
ui->stackedWidget->setCurrentIndex(2);
}
}
void TimerTaskWidget::wait(int ms)
{
QElapsedTimer t;
t.start();
while (t.elapsed() < ms) {
QCoreApplication::processEvents();
QThread::msleep(1); // 添加一点延迟,避免循环过于频繁
}
}
void TimerTaskWidget::onNetworkFinish()
{
if (reply->error() == QNetworkReply::NoError) {
QByteArray responseData = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
QJsonObject jsonObj = jsonDoc.object();
QString datetimeString = jsonObj["datetime"].toString();
m_BeiJingTime = QDateTime::fromString(datetimeString, Qt::ISODate);
m_IsGetSucceed = 1;
} else {
m_IsGetSucceed = 0;
qDebug() << "Error:" << reply->errorString();
}
}
void TimerTaskWidget::on_pushButton_clicked()
{
emit sendTimerStop();
emit sendTimeStr("");
this->hide();
}
void TimerTaskWidget::on_comboBox_DingShi_TimeType_currentIndexChanged(int index)
{
if (index == 0) {
ui->dateTimeEdit_DingShi->setDateTime(QDateTime::currentDateTime());
}
if (index == 1) {
m_IsGetSucceed = -1;
getBeiJingTimer();
while (m_IsGetSucceed < 0) {
wait(10);
if (m_IsGetSucceed == 1) {
if (!m_BeiJingTime.isNull()) {
ui->dateTimeEdit_DingShi->setDateTime(m_BeiJingTime);
}
}
else if (m_IsGetSucceed == 0) {
QMessageBox::warning(this, "警告", "北京时间获取失败,或者网络无连接!");
return;
}
}
}
}
void TimerTaskWidget::on_comboBox_JiHua_TimeType_currentIndexChanged(int index)
{
if (index == 0) {
ui->dateTimeEdit_JiHua->setDateTime(QDateTime::currentDateTime());
}
if (index == 1) {
m_IsGetSucceed = -1;
getBeiJingTimer(); //获取北京时间
while (m_IsGetSucceed < 0) { //循环等待
wait(10);
if (m_IsGetSucceed == 1) {
if (!m_BeiJingTime.isNull()) {
ui->dateTimeEdit_JiHua->setDateTime(m_BeiJingTime);
}
}
else if (m_IsGetSucceed == 0) {
QMessageBox::warning(this, "警告", "北京时间获取失败,或者网络无连接!");
return;
}
}
}
}
#ifndef TIMERHANDLER_H
#define TIMERHANDLER_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct DaoJiShiData { //倒计时
int day; //天
int hour; //时
int minute; //分
int second; //秒
bool isRepeat; //是否重复
qint64 daoJiShiNumber;
};
struct DingShiData { //定时器
int timeType; //0本地 1北京
QDateTime time;
int isRepeat; //是否重复 0只运行一次 1每天运行 2自定义
};
struct JiHuaData { //计划器
int timeType; //0本地 1北京
QDateTime time;
};
class TimerHandler : public QObject
{
Q_OBJECT
public:
explicit TimerHandler(QObject *parent = nullptr);
void getBeiJingTimer();
signals:
void sendTimerStart();
void sendTimeStr(QString str);
public slots:
void onInit();
void onTimerOut();
void onTimerHandlerStart(int type, const QVariant& var);
void onNetworkFinish();
void onReadStop();
private:
void startTimeRun();
void stopTimeRun();
private:
bool m_IsStart;
QTimer* m_Timer;
QNetworkAccessManager* networkManager;
QNetworkReply* reply;
JiHuaData m_JiHuaData;
DingShiData m_DingShiData;
DaoJiShiData m_DaoJiShiData;
QDateTime m_BeiJingTime;
int m_Type;
int m_IsGetSucceed; // -1开始等待 1获取成功 0获取失败 -2循环等待
};
Q_DECLARE_METATYPE(DaoJiShiData)
Q_DECLARE_METATYPE(DingShiData)
Q_DECLARE_METATYPE(JiHuaData)
#endif // TIMERHANDLER_H
#include "timerhandler.h"
TimerHandler::TimerHandler(QObject *parent) : QObject(parent)
{
QThread* t = new QThread();
this->moveToThread(t);
connect(t, SIGNAL(started()), this, SLOT(onInit()));
t->start();
m_IsStart = false;
}
void TimerHandler::getBeiJingTimer()
{
QNetworkRequest request(QUrl("http://worldtimeapi.org/api/timezone/Asia/Shanghai"));
reply = networkManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(onNetworkFinish()));
m_IsGetSucceed = -1;
m_BeiJingTime = QDateTime();
}
void TimerHandler::onInit()
{
networkManager = new QNetworkAccessManager();
m_Timer = new QTimer();
connect(m_Timer, SIGNAL(timeout()), this, SLOT(onTimerOut()));
m_Timer->setInterval(1);
}
void TimerHandler::onTimerOut()
{
//qDebug() << "onTimerOut" << QThread::currentThread() << m_IsStart;
if (!m_IsStart) return;
if (m_Type == 0) {
QDateTime time = QDateTime::currentDateTime();
// qDebug() <
if (m_JiHuaData.timeType == 0) {
time = QDateTime::currentDateTime();
}
else if (m_JiHuaData.timeType == 1) {
if (m_IsGetSucceed == -1) {
getBeiJingTimer();
m_IsGetSucceed = -2; //等待
return;
}
else if (m_IsGetSucceed == -2) {
//如果一直没有进入finish就一直退出
return;
}
else if (m_IsGetSucceed == 1) {
time = m_BeiJingTime;
m_IsGetSucceed = -1;
}
else if (m_IsGetSucceed == 0) {
stopTimeRun();
qDebug() <<"北京时间获取失败,或者网络无连接!";
//QMessageBox::warning(0, "警告", "北京时间获取失败,或者网络无连接!");
return;
}
}
if (time.toMSecsSinceEpoch() >= m_JiHuaData.time.toMSecsSinceEpoch()) {
qDebug() << "计划时间到!!!!" << time << m_JiHuaData.time;
emit sendTimerStart();
stopTimeRun();
return;
}
}
else if (m_Type == 1) {
QDateTime time = QDateTime::currentDateTime();;
if (m_DingShiData.timeType == 0) {
time = QDateTime::currentDateTime();
}
else if (m_DingShiData.timeType == 1) {
if (m_IsGetSucceed == -1) {
getBeiJingTimer();
m_IsGetSucceed = -2; //等待
return;
}
else if (m_IsGetSucceed == -2) {
//如果一直没有进入finish就一直退出
return;
}
else if (m_IsGetSucceed == 1) {
time = m_BeiJingTime;
m_IsGetSucceed = -1;
}
else if (m_IsGetSucceed == 0) {
stopTimeRun();
qDebug() <<"北京时间获取失败,或者网络无连接!";
// QMessageBox::warning(0, "警告", "北京时间获取失败,或者网络无连接!");
return;
}
}
if (time >= m_DingShiData.time && m_DingShiData.isRepeat == 0) {
qDebug() << "定时器时间到!!!!" << time << m_DingShiData.time;
emit sendTimerStart();
stopTimeRun();
return;
}
if (m_DingShiData.isRepeat == 1) {
if (time >= m_DingShiData.time) {
qDebug() << "定时器时间到!!!!" << time << m_DingShiData.time;
emit sendTimerStart();
m_DingShiData.time = m_DingShiData.time.addDays(1);
}
}
}
else if (m_Type == 2) {
if (m_DaoJiShiData.daoJiShiNumber % 1000 == 0) {
emit sendTimeStr(QString("倒计时:%1S\n开始运行").arg(m_DaoJiShiData.daoJiShiNumber / 1000));
}
m_DaoJiShiData.daoJiShiNumber--;
if (m_DaoJiShiData.daoJiShiNumber <= 0) {
qDebug() << "倒计时时间到!!!!" << m_DaoJiShiData.daoJiShiNumber;
emit sendTimeStr(QString(""));
emit sendTimerStart();
if (m_DaoJiShiData.isRepeat) {
m_IsStart = true;
m_DaoJiShiData.daoJiShiNumber = ((m_DaoJiShiData.day * 24 * 60 * 60) + (m_DaoJiShiData.hour * 60 * 60) + (m_DaoJiShiData.minute * 60) + m_DaoJiShiData.second);
m_DaoJiShiData.daoJiShiNumber *= 1000;
}
else {
stopTimeRun();
return;
}
}
}
}
void TimerHandler::onTimerHandlerStart(int type, const QVariant &var)
{
m_Type = type;
if (type == 0) {
m_JiHuaData = var.value<JiHuaData>();
emit sendTimeStr(QString("计划:%1 开始运行").arg(m_JiHuaData.time.toString("yyyy-MM-dd\nhh:mm:ss")));
}
else if (type == 1) {
m_DingShiData = var.value<DingShiData>();
QString str = (m_DingShiData.isRepeat == 0) ? "只运行一次" : "每天运行";
emit sendTimeStr(QString("定时:%1 \n%2 开始运行").arg(m_DingShiData.time.toString("hh:mm:ss")).arg(str));
}
else if (type == 2) {
m_DaoJiShiData = var.value<DaoJiShiData>();
}
//qDebug() << "开始计时" << type;
startTimeRun();
}
void TimerHandler::onNetworkFinish()
{
if (reply->error() == QNetworkReply::NoError) {
QByteArray responseData = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
QJsonObject jsonObj = jsonDoc.object();
QString datetimeString = jsonObj["datetime"].toString();
m_BeiJingTime = QDateTime::fromString(datetimeString, Qt::ISODate);
m_IsGetSucceed = 1;
//qDebug() << "m_BeiJingTime" <
} else {
m_IsGetSucceed = 0;
qDebug() << "Error:" << reply->errorString();
}
}
void TimerHandler::onReadStop()
{
stopTimeRun();
}
void TimerHandler::startTimeRun()
{
m_IsGetSucceed = -1;
m_IsStart = true;
if (!m_Timer->isActive()) {
m_Timer->start();
}
}
void TimerHandler::stopTimeRun()
{
m_IsGetSucceed = -1;
m_IsStart = false;
if (m_Timer->isActive()) {
m_Timer->stop();
emit sendTimeStr("定时器被中断运行!");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TimerTaskWidget</class>
<widget class="QWidget" name="TimerTaskWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>329</width>
<height>217</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>329</width>
<height>217</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>329</width>
<height>217</height>
</size>
</property>
<property name="windowTitle">
<string>TimerTaskWidget</string>
</property>
<layout class="QGridLayout" name="gridLayout_14">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>5</number>
</property>
<property name="topMargin">
<number>5</number>
</property>
<property name="rightMargin">
<number>5</number>
</property>
<property name="bottomMargin">
<number>5</number>
</property>
<property name="horizontalSpacing">
<number>20</number>
</property>
<property name="verticalSpacing">
<number>6</number>
</property>
<item row="1" column="0" colspan="3">
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="page_JiHua">
<layout class="QGridLayout" name="gridLayout_9">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_6">
<item row="1" column="0" colspan="2">
<widget class="QDateTimeEdit" name="dateTimeEdit_JiHua">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="font">
<font>
<pointsize>15</pointsize>
</font>
</property>
<property name="displayFormat">
<string>yyyy/M/d H:mm:ss</string>
</property>
<property name="calendarPopup">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>选择时间类型:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBox_JiHua_TimeType">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<item>
<property name="text">
<string>本地时间</string>
</property>
</item>
<item>
<property name="text">
<string>北京时间</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_DingShi">
<layout class="QGridLayout" name="gridLayout_12">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_11">
<item row="1" column="0" colspan="2">
<widget class="QDateTimeEdit" name="dateTimeEdit_DingShi">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="font">
<font>
<pointsize>15</pointsize>
</font>
</property>
<property name="displayFormat">
<string>H:mm:ss</string>
</property>
<property name="calendarPopup">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>选择时间类型:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBox_DingShi_TimeType">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<item>
<property name="text">
<string>本地时间</string>
</property>
</item>
<item>
<property name="text">
<string>北京时间</string>
</property>
</item>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_7">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>重复:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="comboBox_DingShi">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<item>
<property name="text">
<string>只运行一次</string>
</property>
</item>
<item>
<property name="text">
<string>每天运行</string>
</property>
</item>
<item>
<property name="text">
<string>自定义</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_DaoJiShi">
<layout class="QGridLayout" name="gridLayout_10">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_8">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_7">
<property name="horizontalSpacing">
<number>10</number>
</property>
<property name="verticalSpacing">
<number>5</number>
</property>
<item row="1" column="0">
<widget class="QLabel" name="label_4">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>分:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="spinBox_Day">
<property name="minimumSize">
<size>
<width>80</width>
<height>35</height>
</size>
</property>
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="maximum">
<number>99999</number>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_6">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>天:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QSpinBox" name="spinBox_Hour">
<property name="minimumSize">
<size>
<width>80</width>
<height>35</height>
</size>
</property>
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="maximum">
<number>99999</number>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>时:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="label_5">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>秒:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="spinBox_Minute">
<property name="minimumSize">
<size>
<width>80</width>
<height>35</height>
</size>
</property>
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="maximum">
<number>99999</number>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QSpinBox" name="spinBox_Second">
<property name="minimumSize">
<size>
<width>80</width>
<height>35</height>
</size>
</property>
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="maximum">
<number>99999</number>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<layout class="QGridLayout" name="gridLayout_13">
<property name="leftMargin">
<number>15</number>
</property>
<item row="0" column="0">
<widget class="QCheckBox" name="checkBox_DaoJiShi">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>重复运行</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
<item row="0" column="2">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" 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="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="0" column="1">
<widget class="QRadioButton" name="radioButton_DaoJiShi">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>倒计时器</string>
</property>
<property name="autoRepeat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0" colspan="3">
<layout class="QGridLayout" name="gridLayout_4">
<property name="horizontalSpacing">
<number>15</number>
</property>
<property name="verticalSpacing">
<number>0</number>
</property>
<item row="0" column="0">
<spacer name="horizontalSpacer_5">
<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="3">
<spacer name="horizontalSpacer_6">
<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">
<widget class="QPushButton" name="pushButton_Save">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>开始计时</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="pushButton">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>退出</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<spacer name="horizontalSpacer_7">
<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">
<widget class="QRadioButton" name="radioButton_JiHua">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>计划器</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="autoRepeat">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="0" column="1">
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="2">
<spacer name="horizontalSpacer_4">
<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="0">
<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 row="0" column="1">
<widget class="QRadioButton" name="radioButton_DingShi">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>定时器</string>
</property>
<property name="autoRepeat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
m_TimerTaskWidget = new TimerTaskWidget();
m_TimerTaskWidget->setWindowModality(Qt::ApplicationModal);
m_TimerTaskWidget->setAttribute(Qt::WA_ShowModal, true);
connect(m_TimerTaskWidget, SIGNAL(sendTimerStart()), this, SLOT(onReadTimerStart()));
connect(m_TimerTaskWidget, SIGNAL(sendTimeStr(QString)), this, SLOT(onReadTimerStr(QString)));
就这么简单
1.sendTimerStart是到点了通知,sendTimeStr是发送倒计时时间。
2.定时采用多线程操作防止卡主线程。
如果对您有所帮助,请帮忙点个小赞赞哦!