04-4_Qt 5.9 C++开发指南_时间日期与定时器

news/2025/2/15 21:26:53/

文章目录

  • 1. 时间日期相关的类
  • 2. 源码
    • 2.1 可视化UI设计
    • 2.2 dialog.h
    • 2.3 dialog.cpp

1. 时间日期相关的类

时间日期是经常遇到的数据类型,Qt 中时间日期类型的类如下。

  • QTime:时间数据类型,仅表示时间,如 15:23:13。

  • QDate:日期数据类型,仅表示日期,如2017-4-5.

  • QDateTime:日期时间数据类型,表示日期和时间,如2017-03-23 08:12:43.

    Qt 中有专门用于日期、时间编辑和显示的界面组件,介绍如下。

  • QTimeEdit: 编辑和显示时间的组件。

  • QDateEdit:编辑和显示日期的组件

  • QDateTimeEdit:编辑和显示日期时间的组件。

  • QCalendarWidget:一个用日历形式选择日期的组件。

定时器是用来处理周期性事件的一种对象,类似于硬件定时器。例如设置一个定时器的定时周期为 1000 毫秒,那么每 1000 毫秒就会发射定时器的 timeout()信号,在信号关联的槽函数里就可以做相应的处理。Qt 中的定时器类是 QTimer,它直接从QObiect 类继承而来,不是界面组件类。

实例程序 sammp4_5 演示这些时间日期相关类的使用,其运行时界面如图4-5 所示。

在这里插入图片描述

关于”日期时间数据与字符串之间的转换“、”QCalendarWidget 日历组件“、”定时器的使用“请参考源码及《Qt5.9 c++开发指南》

2. 源码

2.1 可视化UI设计

在这里插入图片描述

2.2 dialog.h

#ifndef DIALOG_H
#define DIALOG_H#include    <QDialog>
#include    <QTimer>
#include    <QTime>namespace Ui {
class Dialog;
}class Dialog : public QDialog
{Q_OBJECT
private:QTimer *fTimer;  //定时器QTime   fTimeCounter;//计时器public:explicit Dialog(QWidget *parent = 0);~Dialog();private slots:void on_timer_timeout(); //定时器中断处理槽函数,手工定义void on_btnGetTime_clicked();//读取当前时间void on_calendarWidget_selectionChanged(); //Calender 选择日期void on_btnSetTime_clicked();  // 读取当前日期时间  按键void on_btnSetDate_clicked(); //设置日期  按键void on_btnSetDateTime_clicked();   //设置日期时间案件void on_btnSetIntv_clicked();   //设置周期  按键void on_btnStart_clicked();  //开始 定时器 按键void on_btnStop_clicked();      //停止定时器按键private:Ui::Dialog *ui;
};#endif // DIALOG_H

2.3 dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"#include    <QDateTime>
#include    <QString>Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog)
{ui->setupUi(this);fTimer=new QTimer(this);  //创建定时器fTimer->stop();fTimer->setInterval(1000);//设置定时周期,单位:毫秒connect(fTimer,SIGNAL(timeout()),this,SLOT(on_timer_timeout())); //关联定时器的信号与槽
}Dialog::~Dialog()
{delete ui;
}void Dialog::on_timer_timeout()
{ //定时器中断响应槽函数QTime curTime=QTime::currentTime(); //获取当前时间ui->LCDHour->display(curTime.hour()); //显示  小时ui->LCDMin->display(curTime.minute());//显示  分钟ui->LCDSec->display(curTime.second());//显示  秒int va=ui->progressBar->value(); //读取progressBar的数值va++;if (va>100)va=0;ui->progressBar->setValue(va); //设置progressBar的数值
}void Dialog::on_btnGetTime_clicked()
{ //获取当前日期时间,为三个专用编辑器设置日期时间数据,并转换为字符串在LineEdit里显示QDateTime curDateTime=QDateTime::currentDateTime(); //读取当前日期时间ui->timeEdit->setTime(curDateTime.time()); //设置时间ui->editTime->setText(curDateTime.toString("hh:mm:ss"));//转换为字符串显示ui->dateEdit->setDate(curDateTime.date());//设置日期ui->editDate->setText(curDateTime.toString("yyyy-MM-dd"));//转换为字符串显示ui->dateTimeEdit->setDateTime(curDateTime);//设置日期时间ui->editDateTime->setText(curDateTime.toString("yyyy-MM-dd hh:mm:ss"));//转换为字符串显示
}void Dialog::on_calendarWidget_selectionChanged()
{ //在日历上选择日期QDate dt=ui->calendarWidget->selectedDate(); //读取选择的日期时间QString str=dt.toString("yyyy年M月d日");//转换为字符串ui->editCalendar->setText(str); //字符串显示日期
}void Dialog::on_btnSetTime_clicked()
{ //字符串转换为QTimeQString str=ui->editTime->text(); //读取字符串表示的时间//    str=str.trimmed();//去掉空格if (!str.isEmpty()){QTime tm=QTime::fromString(str,"hh:mm:ss"); //从字符串转换为QTimeui->timeEdit->setTime(tm); //设置时间}
}void Dialog::on_btnSetDate_clicked()
{//字符串转换为 QDateQString str=ui->editDate->text(); //读取字符串表示的日期
//    str=str.trimmed();//去掉空格if (!str.isEmpty()){QDate dt=QDate::fromString(str,"yyyy-MM-dd");//从字符串转换为 QDateui->dateEdit->setDate(dt);//设置日期}
}void Dialog::on_btnSetDateTime_clicked()
{//字符串转换为 QDateTimeQString str=ui->editDateTime->text();//读取字符串表示的日期str=str.trimmed();//去掉空格if (!str.isEmpty()){QDateTime datetime=QDateTime::fromString(str,"yyyy-MM-dd hh:mm:ss"); //从字符串转换为 QDateTimeui->dateTimeEdit->setDateTime(datetime);//设置日期时间}
}void Dialog::on_btnSetIntv_clicked()
{ //设置定时器周期fTimer->setInterval(ui->spinBoxIntv->value()); //设置定时器的周期
}void Dialog::on_btnStart_clicked()
{fTimer->start();//定时器开始工作fTimeCounter.start();//计时器开始工作
//更新各按键的状态ui->btnStart->setEnabled(false);ui->btnStop->setEnabled(true);ui->btnSetIntv->setEnabled(false);
}void Dialog::on_btnStop_clicked()
{fTimer->stop(); //定时器停止int tmMsec=fTimeCounter.elapsed();//毫秒数int ms=tmMsec%1000; //余数毫秒int sec=tmMsec/1000; //整秒QString str=QString::asprintf("流逝时间:%d 秒,%d 毫秒",sec,ms);ui->LabElapsTime->setText(str); //显示流逝的时间ui->btnStart->setEnabled(true); //更新按键状态ui->btnStop->setEnabled(false);ui->btnSetIntv->setEnabled(true);
}

http://www.ppmy.cn/news/1013414.html

相关文章

yolo txt 转 labelme json 格式

talk is cheap show me the code! def convert_txt_to_labelme_json(txt_path, image_path, output_dir, image_fmt.jpg):# txt 转labelme json# 将yolo的txt转labelme jsontxts glob.glob(os.path.join(txt_path, "*.txt"))for txt in txts:labelme_json {versio…

java文件

一.File类 二.扫描指定目录&#xff0c;并找到名称中包含指定字符的所有普通文件&#xff08;不包含目录&#xff09;&#xff0c;并且后续询问用户是否要删除该文件 我的代码: import java.io.File; import java.io.IOException; import java.util.Scanner;public class Tes…

SQLAlchemy与标准SQL相比有哪些优点?

让我来给你讲讲SQLAlchemy和标准SQL相比有哪些优点吧&#xff01; 首先&#xff0c;我们要知道&#xff0c;SQLAlchemy是一个Python的SQL工具包和对象关系映射&#xff08;ORM&#xff09;系统&#xff0c;它把Python的面向对象编程&#xff08;OOP&#xff09;的理念带入了数…

机器学习笔记之优化算法(十一)凸函数铺垫:梯度与方向导数

机器学习笔记之优化算法——凸函数铺垫&#xff1a;梯度与方向导数 引言回顾&#xff1a;偏导数方向余弦方向导数方向导数的几何意义方向导数的定义 方向导数与偏导数之间的关联关系证明过程 梯度 ( Gradient ) (\text{Gradient}) (Gradient) 引言 本节作为介绍凸函数的铺垫&a…

FastAPI 构建 API 高性能的 web 框架(一)

如果要部署一些大模型一般langchainfastapi&#xff0c;或者fastchat&#xff0c; 先大概了解一下fastapi,本篇主要就是贴几个实际例子。 官方文档地址&#xff1a; https://fastapi.tiangolo.com/zh/ 1 案例1:复旦MOSS大模型fastapi接口服务 来源&#xff1a;大语言模型工程…

封装上传文件组件(axios,onUploadProgress,取消请求)

目录 定时模拟进度条 方法 A.axios B.xhr 取消请求​​​​​​​ 完整代码 A.自定义上传组件 B.二次封装组件 情况 增加cancelToken不生效&#xff0c;刷新页面 进度条太快->设置浏览器网速 定时模拟进度条 startUpload() {if (!this.file) return;const totalS…

0基础学习VR全景平台篇 第79篇:全景相机-泰科易如何直播推流

泰科易科技是中国的一家研发全景相机的高科技公司&#xff0c;前不久&#xff0c;在2020世界VR产业大会上发布了新一代5G VR直播影像采集终端--360starlight。以其出色的夜景成像效果和一“部”到位的直播方案重新定义了VR慢直播相机&#xff0c;对行业具有高度借鉴意义。 本文…

SpringCloud项目打包注意事项以及可能出错的几种情况

SpringCloud项目打包注意事项和可能出错的几种情况 1、检查子模块中的 parent的pom文件路径 \<relativePath/\>2、检查打包插件的位置3、检查module是否重复引用 欢迎访问我的个人博客&#xff1a;https://wk-blog.vip 1、检查子模块中的 parent的pom文件路径 <relat…