-
例子
#ifndef WIDGET_H #define WIDGET_H #include <QTime> #include <QTimer> #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_btnGetTime_clicked(); void on_btnSetTime_clicked(); void on_btnSetDate_clicked(); void on_btnSetDateTime_clicked(); void on_calendarWidget_selectionChanged(); /** * @brief on_timer_timeOut * @note 定时器超时的槽函数 */ void on_timer_timeOut(); void on_btnStart_clicked(); void on_btnStop_clicked(); void on_btnSetInterval_clicked(); private: Ui::Widget *ui; /** * @brief timer 定时器timer */ QTimer *timer; /** * @brief timeCounter 计时器 */ QTime timeCounter; }; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h" #include <QTimer> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); timer = new QTimer(this); timer->stop(); connect(timer, SIGNAL(timeout()), this, SLOT(on_timer_timeOut())); } Widget::~Widget() { delete ui; } /** * @brief Widget::on_btnGetTime_clicked * @note 获取本地时间,并显示在文本框中 */ void Widget::on_btnGetTime_clicked() { QDateTime curDateTime = QDateTime::currentDateTime(); QString date = curDateTime.toString("yyyy-MM-dd"); QString time = curDateTime.toString("HH:mm:ss"); QString dateTime = curDateTime.toString("yyyy-MM-dd HH:mm:ss"); ui->editDate->setText(date); ui->dateEdit->setDate(curDateTime.date()); ui->editTime->setText(time); ui->timeEdit->setTime(curDateTime.time()); ui->editDateTime->setText(dateTime); ui->dateTimeEdit->setDateTime(curDateTime); } /** * @brief Widget::on_btnSetTime_clicked * @note 获取文本框中的内容,然后转换为时间 */ void Widget::on_btnSetTime_clicked() { QString timeStr = ui->editTime->text(); QTime time = QTime::fromString(timeStr, "HH:mm:ss"); ui->timeEdit->setTime(time); } /** * @brief Widget::on_btnSetDate_clicked * @note 获取文本框中的内容,然后转换为日期 */ void Widget::on_btnSetDate_clicked() { QString dateStr = ui->editDate->text(); QDate date = QDate::fromString(dateStr, "yyyy-MM-dd"); ui->dateEdit->setDate(date); } /** * @brief Widget::on_btnSetDateTime_clicked * @note 获取文本框中的内容,然后转换为时间和日期 */ void Widget::on_btnSetDateTime_clicked() { QString dateTimeStr = ui->editDateTime->text(); QDateTime dateTime = QDateTime::fromString(dateTimeStr, "yyyy-MM-dd HH:mm:ss"); ui->dateTimeEdit->setDateTime(dateTime); } /** * @brief Widget::on_calendarWidget_selectionChanged * @note 获取日期控件的所选择的日期,然后设置到文本框 */ void Widget::on_calendarWidget_selectionChanged() { QDate date = ui->calendarWidget->selectedDate(); ui->editDateCalendar->setText(date.toString("yyyy-MM-dd")); } /** * @brief Widget::on_timer_timeOut * @note timer的超时事件,QT的timer要手动去定义timer、槽,手动去写connect */ void Widget::on_timer_timeOut() { ui->lcdH->display(QTime::currentTime().hour()); ui->lcdM->display(QTime::currentTime().minute()); ui->lcdS->display(QTime::currentTime().second()); } /** * @brief Widget::on_btnStart_clicked * @note 启动timer,并设置控件是否禁用 */ void Widget::on_btnStart_clicked() { timer->start(); timeCounter.start(); ui->btnStart->setEnabled(false); ui->btnSetInterval->setEnabled(false); ui->btnStop->setEnabled(true); } /** * @brief Widget::on_btnStop_clicked * @note 停止timer,并设置控件是否禁用 */ void Widget::on_btnStop_clicked() { timer->stop(); int time = timeCounter.elapsed(); int second = time / 1000; int miniSec = time % 1000; QString str = QString::asprintf("%d\" %d",second, miniSec); ui->label_8->setText(str); ui->btnStart->setEnabled(true); ui->btnSetInterval->setEnabled(true); ui->btnStop->setEnabled(false); } /** * @brief Widget::on_btnSetInterval_clicked * @note 设置timer的interval */ void Widget::on_btnSetInterval_clicked() { int value = ui->spinBox->value(); timer->setInterval(value); }
-
界面