diff --git a/QtDateTimeEx/ExDateTime.cpp b/QtDateTimeEx/ExDateTime.cpp new file mode 100644 index 0000000..f304658 --- /dev/null +++ b/QtDateTimeEx/ExDateTime.cpp @@ -0,0 +1,92 @@ +#include "ExDateTime.h" +#include "ui_ExDateTime.h" +#include <QDateTime> + +ExDateTime::ExDateTime(QWidget *parent) : + QWidget(parent), + ui(new Ui::ExDateTime) +{ + ui->setupUi(this); + setWindowTitle(QObject::tr("时间日期(QTime/QDate/QDateTime)和定时器(QTimer)")); + + //editDate控件在UI设计师里面,选中了calendarPopup (日历弹出的属性)和displayFormat显示格式 + + m_timer = new QTimer(this); + m_timer->stop(); //关闭定时器 + m_timer->setInterval(1000); //设定定时周期, 单位 毫秒 + connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimerOut())); +} + +ExDateTime::~ExDateTime() +{ + delete ui; +} + +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//获取当前日期和时间,该日期时间栏里面显示 +void ExDateTime::on_btnGetDateTime_clicked() +{ + QDateTime currDateTime = QDateTime::currentDateTime(); + ui->timeEdit->setTime(currDateTime.time()); + ui->editTime->setText(currDateTime.toString("hh:mm:ss:zzz")); + ui->dateEdit->setDate(currDateTime.date()); + ui->editDate->setText(currDateTime.toString("yyyy-MM-dd")); + ui->dateTimeEdit->setDateTime(currDateTime); + ui->editDateTime->setText(currDateTime.toString("yyyy-MM-dd hh:mm:ss:zzz")); + ui->labCurrDataTime->setText(currDateTime.toString("yyyy-MM-dd hh:mm:ss:zzz")); +} + +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//计时器开始 +void ExDateTime::on_btnStatrt_clicked() +{ + m_time.start(); //计时器开始 + m_timer->start(); //定时器开始 + ui->btnStatrt->setEnabled(false); //开始按下之后,开始按钮禁用 + ui->btnStop->setEnabled(true); //同时结束按钮可用 + ui->btnPeriod->setEnabled(false); //设定周期的按钮为禁用 + ui->labGo->setText(QString("时间流逝在后台计算中...")); +} + +//计时器结束 +void ExDateTime::on_btnStop_clicked() +{ + m_timer->stop(); //定时器停止 + ui->btnStop->setEnabled(false); //结束按下之后,结束按钮禁用 + ui->btnStatrt->setEnabled(true); //同时开始按钮可用 + + int tmMsec = m_time.elapsed(); //计时器Time没有对应的stop(), elapsed获取它的毫秒数 + int ms = tmMsec % 1000; //经过的毫秒 + int sec = tmMsec / 1000; //经过的秒 + ui->btnPeriod->setEnabled(true); //设定周期的按钮为可用 + ui->labGo->setText(QString("时间已经流逝:%1 秒 %2 毫秒").arg(sec).arg(ms)); +} + +//处理定时器的槽函数 +void ExDateTime::onTimerOut() +{ + QTime currTime = QTime::currentTime(); + ui->lcdHH->display(currTime.hour()); //多种显示时间方法 + ui->lcdmm->display(currTime.toString("mm")); + ui->lcdSS->display(currTime.toString("ss")); + + int val = ui->progressBar->value(); //设置进度条同时增加 + val++; + if (val > 100) + val = 0; + ui->progressBar->setValue(val); +} + +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//选择日历时间 +void ExDateTime::on_calendarWidget_selectionChanged() +{ + QDate date =ui->calendarWidget->selectedDate(); + ui->editChoose->setText(date.toString("yyyy年MM月dd日")); +} + +//设定定时器QTimer周期 +void ExDateTime::on_btnPeriod_clicked() +{ + m_timer->setInterval(ui->spinBox->value()); +} diff --git a/QtDateTimeEx/ExDateTime.h b/QtDateTimeEx/ExDateTime.h new file mode 100644 index 0000000..833cab5 --- /dev/null +++ b/QtDateTimeEx/ExDateTime.h @@ -0,0 +1,40 @@ +#ifndef EXDATETIME_H +#define EXDATETIME_H + +#include <QWidget> +#include <QTimer> +#include <QTime> + +namespace Ui { +class ExDateTime; +} + +class ExDateTime : public QWidget +{ + Q_OBJECT + +public: + explicit ExDateTime(QWidget *parent = nullptr); + ~ExDateTime(); + +private slots: + void on_btnGetDateTime_clicked(); + void on_btnStatrt_clicked(); + void on_btnStop_clicked(); + void onTimerOut(); //处理计时器的信号的槽函数 + + void on_calendarWidget_selectionChanged(); + + void on_btnPeriod_clicked(); + +private: + Ui::ExDateTime *ui; + + QTimer* m_timer; //定时器(不可见控件) + QTime m_time; //计时器(此处用作) + + + +}; + +#endif // EXDATETIME_H diff --git a/QtDateTimeEx/ExDateTime.ui b/QtDateTimeEx/ExDateTime.ui new file mode 100644 index 0000000..ec931fe --- /dev/null +++ b/QtDateTimeEx/ExDateTime.ui @@ -0,0 +1,211 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>ExDateTime</class> + <widget class="QWidget" name="ExDateTime"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>843</width> + <height>333</height> + </rect> + </property> + <property name="windowTitle"> + <string>ExDateTime</string> + </property> + <layout class="QGridLayout" name="gridLayout_7"> + <item row="0" column="0"> + <widget class="QGroupBox" name="groupBox"> + <property name="title"> + <string>日期时间(QTime、QDate、QDateTime)</string> + </property> + <layout class="QGridLayout" name="gridLayout_2"> + <item row="0" column="0"> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0" colspan="2"> + <widget class="QPushButton" name="btnGetDateTime"> + <property name="text"> + <string>获取当前时间日期</string> + </property> + </widget> + </item> + <item row="4" column="0"> + <widget class="QLabel" name="labDateTime"> + <property name="text"> + <string>日期时间</string> + </property> + </widget> + </item> + <item row="3" column="0"> + <widget class="QLabel" name="labDate"> + <property name="text"> + <string>日 期</string> + </property> + </widget> + </item> + <item row="2" column="2"> + <widget class="QLineEdit" name="editTime"/> + </item> + <item row="3" column="2"> + <widget class="QLineEdit" name="editDate"/> + </item> + <item row="4" column="2"> + <widget class="QLineEdit" name="editDateTime"/> + </item> + <item row="0" column="2" colspan="2"> + <widget class="QLabel" name="labCurrDataTime"> + <property name="text"> + <string>当前时间:</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="labTime"> + <property name="text"> + <string>时 间</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QTimeEdit" name="timeEdit"> + <property name="minimumSize"> + <size> + <width>150</width> + <height>0</height> + </size> + </property> + <property name="calendarPopup"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QDateEdit" name="dateEdit"> + <property name="displayFormat"> + <string>yyyy年MM月dd日</string> + </property> + <property name="calendarPopup"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="4" column="1"> + <widget class="QDateTimeEdit" name="dateTimeEdit"/> + </item> + </layout> + </item> + </layout> + </widget> + </item> + <item row="0" column="1" rowspan="2"> + <widget class="QGroupBox" name="groupBox_3"> + <property name="title"> + <string>日历组件(Calendar Widget)</string> + </property> + <layout class="QGridLayout" name="gridLayout_6"> + <item row="0" column="0"> + <layout class="QGridLayout" name="gridLayout_5"> + <item row="0" column="0"> + <widget class="QLabel" name="labChoose"> + <property name="text"> + <string>选择的日期:</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLineEdit" name="editChoose"/> + </item> + <item row="1" column="0" colspan="2"> + <widget class="QCalendarWidget" name="calendarWidget"/> + </item> + </layout> + </item> + </layout> + </widget> + </item> + <item row="1" column="0"> + <widget class="QGroupBox" name="groupBox_2"> + <property name="title"> + <string>定时器(QTimer)</string> + </property> + <layout class="QGridLayout" name="gridLayout_4"> + <item row="0" column="0"> + <layout class="QGridLayout" name="gridLayout_3"> + <item row="1" column="0"> + <widget class="QLabel" name="labPeriod"> + <property name="text"> + <string>定时周期</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QSpinBox" name="spinBox"> + <property name="suffix"> + <string>ms</string> + </property> + <property name="prefix"> + <string/> + </property> + <property name="maximum"> + <number>9999999</number> + </property> + <property name="value"> + <number>1000</number> + </property> + </widget> + </item> + <item row="2" column="2"> + <widget class="QLCDNumber" name="lcdSS"/> + </item> + <item row="0" column="2"> + <widget class="QLabel" name="labGo"> + <property name="text"> + <string>时间已流逝:</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLCDNumber" name="lcdmm"/> + </item> + <item row="2" column="0"> + <widget class="QLCDNumber" name="lcdHH"/> + </item> + <item row="1" column="2"> + <widget class="QPushButton" name="btnPeriod"> + <property name="text"> + <string>设置周期</string> + </property> + </widget> + </item> + <item row="0" column="0"> + <widget class="QPushButton" name="btnStatrt"> + <property name="text"> + <string>开始</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QPushButton" name="btnStop"> + <property name="text"> + <string>停止</string> + </property> + </widget> + </item> + <item row="3" column="0" colspan="3"> + <widget class="QProgressBar" name="progressBar"> + <property name="value"> + <number>3</number> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <layoutdefault spacing="6" margin="11"/> + <resources/> + <connections/> +</ui> diff --git a/QtDateTimeEx/QT_win_32x32.ico b/QtDateTimeEx/QT_win_32x32.ico new file mode 100644 index 0000000..0ee0568 Binary files /dev/null and b/QtDateTimeEx/QT_win_32x32.ico differ diff --git a/QtDateTimeEx/QtDateTimeEx.pro b/QtDateTimeEx/QtDateTimeEx.pro new file mode 100644 index 0000000..250d984 --- /dev/null +++ b/QtDateTimeEx/QtDateTimeEx.pro @@ -0,0 +1,42 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-08-24T15:33:12 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtDateTimeEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExDateTime.cpp + +HEADERS += \ + ExDateTime.h + +FORMS += \ + ExDateTime.ui + +RC_ICONS += QT_win_32x32.ico + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtDateTimeEx/main.cpp b/QtDateTimeEx/main.cpp new file mode 100644 index 0000000..f69fdd8 --- /dev/null +++ b/QtDateTimeEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExDateTime.h" +#include <QApplication> + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExDateTime w; + w.show(); + + return a.exec(); +}