0004:Qt常用类 - QTime

article/2025/10/25 22:02:31

Qt常用类 - QTime

1 开发环境

在介绍内容之前,先说明一下开发环境,如下图:
在这里插入图片描述在这里插入图片描述
Qt版本:Qt5.3.2;
Qt开发工具:Qt Creater 3.2.1;
Qt构建工具:Desktop Qt 5.3 MinGW 32bit;
Qt开发平台:Windows 7 64bit。

2 QTime

QTime是Qt中用到的时间类,可以用于对自然时间的记录和计算,但是更多的是对于时间这个概念的记录和计算。首先,将QTime中的常用接口整理出来,分为初始化、获取、计算、流逝的时间、转换为字符串、比较共6类,这样方便学习和理解。

//初始化
QTime();
QTime(int h, int m, int s = 0, int ms = 0);
bool             setHMS(int h, int m, int s, int ms = 0);
bool             isNull() const;
bool             isValid() const;
static QTime     QTime::currentTime();
static QTime     QTime::fromMSecsSinceStartOfDay(int msecs);
static QTime     QTime::fromString(const QString &string, Qt::DateFormat format = Qt::TextDate);
static QTime     QTime::fromString(const QString &string, const QString &format);
static bool      QTime::isValid(int h, int m, int s, int ms = 0);//获取
int              hour() const;
int              minute() const;
int              second() const;
int              msec() const;//计算
QTime            addMSecs(int ms) const;
QTime            addSecs(int s) const;
int              msecsSinceStartOfDay() const;
int              msecsTo(const QTime &t) const;
int              secsTo(const QTime &t) const;//流逝的时间,这是些很有用的接口,需要配合起来使用,可以计算程序耗时等时间
void             start();
void             restart();
int              elapsed() const;//转换字符串
QString          toString(const QString &format) const;
QString          toString(Qt::DateFormat format = Qt::TextDate) const;//比较
bool             operator!=(const QTime &t) const;
bool             operator==(const QTime &t) const;
bool             operator<(const QTime &t) const;
bool             operator<=(const QTime &t) const;
bool             operator>(const QTime &t) const;
bool             operator>=(const QTime &t) const;

下面,是上述接口的使用例程。

#include <QDate>
#include <QThread>#define qPrintableU8(_string)                       (QString(_string).toUtf8().constData())
#define TIME_STRING_FORMAT                          ("hh:mm:ss.zzz")//QTime();
//QTime(int h, int m, int s = 0, int ms = 0);
//bool             setHMS(int h, int m, int s, int ms = 0);
//bool             isNull() const;
//bool             isValid() const;
//static QTime     QTime::currentTime();
//static QTime     QTime::fromMSecsSinceStartOfDay(int msecs);
//static QTime     QTime::fromString(const QString &string, Qt::DateFormat format = Qt::TextDate);
//static QTime     QTime::fromString(const QString &string, const QString &format);
//static bool      QTime::isValid(int h, int m, int s, int ms = 0);
QTime timeNull;
qDebug("timeNull = %02d:%02d:%02d.%03d.", timeNull.hour(), timeNull.minute(), timeNull.second(), timeNull.msec());
qDebug("timeNull is null? %s.", timeNull.isNull() ? "yes" : "no");
qDebug("timeNull is valid? %s.", timeNull.isValid() ? "yes" : "no");
QTime time(20, 20, 20, 20);
qDebug("time = %s.", qPrintableU8(time.toString(TIME_STRING_FORMAT)));
qDebug("time is valid? %s.", QTime::isValid(time.hour(), time.minute(), time.second(), time.msec()) ? "yes" : "no");
QTime timeSystem = QTime::currentTime();
qDebug("timeSystem: %s.", qPrintableU8(timeSystem.toString(TIME_STRING_FORMAT)));
QTime timeFromMs = QTime::fromMSecsSinceStartOfDay(3600 * 1000);
qDebug("timeFromMs: %s.", qPrintableU8(timeFromMs.toString(TIME_STRING_FORMAT)));
QTime timeFs1 = QTime::fromString("20:21:22.023", Qt::ISODate);
qDebug("timeFs1: %s.", qPrintableU8(timeFs1.toString(TIME_STRING_FORMAT)));
QTime timeFs2 = QTime::fromString("22:23:24.025", TIME_STRING_FORMAT);
qDebug("timeFs2: %s.", qPrintableU8(timeFs2.toString(TIME_STRING_FORMAT)));//int              hour() const;
//int              minute() const;
//int              second() const;
//int              msec() const;
//参考上面或下面代码中用到的地方
//...//QTime            addMSecs(int ms) const;
//QTime            addSecs(int s) const;
//int              msecsSinceStartOfDay() const;
//int              msecsTo(const QTime &t) const;
//int              secsTo(const QTime &t) const;
QTime timeBase(0, 0, 0, 0);
QTime timeAddMs = timeBase.addMSecs(3600 * 1000);
qDebug("timeAddMs = %s.", qPrintableU8(timeAddMs.toString(TIME_STRING_FORMAT)));
QTime timeAddS = timeBase.addSecs(3600);
qDebug("timeAddS = %s.", qPrintableU8(timeAddS.toString(TIME_STRING_FORMAT)));
qDebug("msecsSinceStartOfDay = %d.", timeAddS.msecsSinceStartOfDay());
qDebug("msecsTo = %d.", timeBase.msecsTo(timeAddS));
qDebug("secsTo = %d.", timeBase.secsTo(timeAddS));//void             start();
//void             restart();
//int              elapsed() const;
QTime timeElapsed;
timeElapsed.start();
QThread::msleep(100);
qDebug("timeElapsed(start): %dms.", timeElapsed.elapsed());
timeElapsed.restart();
QThread::msleep(200);
qDebug("timeElapsed(restart): %dms.", timeElapsed.elapsed());//QString          toString(const QString &format) const;
//QString          toString(Qt::DateFormat format = Qt::TextDate) const;
QTime timeTs(0, 1, 2, 3);
qDebug("timeTs = %s", qPrintableU8(timeTs.toString("hh:mm:ss.zzz(AP)")));
qDebug("timeTs = %s", qPrintableU8(timeTs.toString(TIME_STRING_FORMAT)));
qDebug("timeTs = %s", qPrintableU8(timeTs.toString(Qt::ISODate)));//bool             operator!=(const QTime &t) const;
//bool             operator==(const QTime &t) const;
//bool             operator<(const QTime &t) const;
//bool             operator<=(const QTime &t) const;
//bool             operator>(const QTime &t) const;
//bool             operator>=(const QTime &t) const;
QTime timeEarlier(0, 0, 0, 0), timeLater(1, 1, 1, 1);
qDebug("timeEarlier != timeLater? %s.", timeEarlier != timeLater ? "yes" : "no");
qDebug("timeEarlier == timeLater? %s.", timeEarlier == timeLater ? "yes" : "no");
qDebug("timeEarlier <  timeLater? %s.", timeEarlier <  timeLater ? "yes" : "no");
qDebug("timeEarlier <= timeLater? %s.", timeEarlier <= timeLater ? "yes" : "no");
qDebug("timeEarlier >  timeLater? %s.", timeEarlier >  timeLater ? "yes" : "no");
qDebug("timeEarlier >= timeLater? %s.", timeEarlier >= timeLater ? "yes" : "no");

在学习过程中,可以将上述代码放到一个按钮的响应函数中,以调试方式运行,就可以在Qt Creater中的应用程序输出窗口看到输出结果了。


http://chatgpt.dhexx.cn/article/Esc2RNLH.shtml

相关文章

QTime

一、描述 QTime 对象包含一个时钟时间&#xff0c;它可以表示为自午夜以来的小时数、分钟数、秒数、毫秒数。 它提供了比较时间和通过添加毫秒数来操纵时间的功能。 QTime 对象应该通过值而不是通过引用 const 来传递。 二、成员函数 1、QString toString(const QString &…

双边滤波+ 通俗自己理解

之前转了一篇文章&#xff1a;http://blog.csdn.net/chenlufei_i/article/details/78892758 详细的讲解了一下双边滤波的原理以及公式&#xff0c;这里主要总结一下自己的通俗理解&#xff0c;所谓双边滤波&#xff0c;就是在均值或者说普通加权滤波&#xff08;如高斯滤波&am…

双边滤波原理与参数的理解+双边滤波matlab实现

基本思路 高斯滤波是一种常用而且简单的降噪算法。但其缺点也很明显&#xff0c;就是会不加区分地将噪声与图像边缘等细节一起平滑处理。 而双边滤波是基于高斯滤波进行改进的方法。在高斯滤波的基础上&#xff0c;双边滤波引入了像素值相似性的权值&#xff0c;使得滤波的结…

双边滤波(Bilateral filter)原理介绍及matlab程序实现

双边滤波 1、原理介绍 双边滤波由C. Tomasi在1998年提出&#xff0c;是一种经典的非线性空间滤波方法。在滤波器稀疏的制定上&#xff0c;双边滤波同时考虑到了输出像素与邻域内其它像素的欧氏距离和取值的差异&#xff0c;即&#xff1a;同时考虑到了空间域和值域间的差别。…

双边滤波器的原理及实现

双边滤波器是什么? 双边滤波(Bilateral filter)是一种可以保边去噪的滤波器。之所以可以达到此去噪效果,是因为滤波器是由两个函数构成。一个函数是由几何空间距离决定滤波器系数。另一个由像素差值决定滤波器系数。可以与其相比较的两个filter:高斯低通滤波器(http://en…

双边滤波原理浅析

前言 双边滤波&#xff08;Bilateral filtering&#xff09;是一种结合了图像像素空间位置关系和像素值大小的非线性组合的保边滤波算法&#xff0c;无需迭代计算&#xff0c;操作简单。从原理上讲&#xff0c;图像领域的滤波通常意义是希望去除噪声&#xff0c;使图像更加平滑…

双边滤波(Bilateral Filter)详解

原理分析: 双边滤波与高斯滤波器相比,对于图像的边缘信息能过更好的保存。其原理为一个与空间距离相关的高斯函数与一个灰度距离相关的高斯函数相乘。 空间距离:指的是当前点与中心点的欧式距离。空间域高斯函数其数学形式为: 其中(xi,yi)为当前点位置,(xc,yc)为中心点…

双边滤波和双边网格

双边滤波 双边滤波器也是一种保边滤波器&#xff0e;和导向图滤波器一样&#xff0c;可以达到在平坦区域进行均值&#xff08;高斯&#xff09;滤波的效果&#xff0c;在边缘不进行滤波的效果&#xff0e;其原理为一个与空间距离相关的高斯函数与一个灰度距离相关的高斯函数相…

bilateral filter双边滤波器的通俗理解

bilateral filter双边滤波器的通俗理解 图像去噪的方法很多&#xff0c;如中值滤波&#xff0c;高斯滤波&#xff0c;维纳滤波等等。但这些降噪方法容易模糊图片的边缘细节&#xff0c;对于高频细节的保护效果并不明显。相比较而言&#xff0c;bilateral filter双边滤波器可以很…

Bilateral Filtering(双边滤波) for SSAO

1. 简介 图像平滑是一个重要的操作&#xff0c;而且有多种成熟的算法。这里主要简单介绍一下Bilateral方法&#xff08;双边滤波&#xff09;&#xff0c;这主要是由于前段时间做了SSAO&#xff0c;需要用bilateral blur 算法进行降噪。Bilateral blur相对于传统的高斯blur来说…

双边滤波与高斯滤波

高斯滤波原理 在数字图像中的高斯噪声主要来源出现在采集期间。由于不良照明或高温引起的传感器噪声。如果一个噪声&#xff0c;它的幅度分布服从高斯分布&#xff0c;而它的功率谱密度又是均匀分布的&#xff0c;则称它为高斯白噪声。高斯滤波器是一种线性滤波器&#xff0c;…

双边滤波(bilateral filter)以及联合双边滤波(joint bilateral filter)

文章目录 双边滤波理论公式代码&#xff08;C&#xff09;数学辅助理解 联合双边滤波&#xff08;joint bilateral filter&#xff09;参考链接 写在最后 双边滤波 自用备忘&#xff0c;若侵则删。 理论公式 利用二维高斯函数生成空间域核&#xff0c;一维高斯函数生成颜色域…

双边滤波(Bilateral filter)

双边滤波器&#xff08;Bilateral filter&#xff09;是一种可以保边去噪的滤波器。可以滤除图像数据中的噪声&#xff0c;且还会保留住图像的边缘、纹理等&#xff08;因噪声是高频信号&#xff0c;边缘、纹理也是高频信息&#xff0c;高斯滤波会在滤除噪声的同时使得边缘模糊…

双边滤波器cv2.bilateralFilter

双边滤波器cv2.bilateralFilter 双边滤波是综合考虑空间信息和色彩信息的滤波方式&#xff0c;在滤波过程中能够有效地保护图像内的边缘信息&#xff0c;双边滤波在计算某一个像素点的新值时&#xff0c;不仅考虑距离信息&#xff08;距离越远&#xff0c;权重越小&#xff09…

opencv-双边滤波

一、双边滤波原理 双边滤波&#xff08;Bilateral Filter&#xff09;是非线性滤波中的一种。这是一种结合图像的空间邻近度与像素值相似度的处理办法。在滤波时&#xff0c;该滤波方法同时考虑空间临近信息与颜色相似信息&#xff0c;在滤除噪声、平滑图像的同时&#xff0c;…

Opencv之图像滤波:6.双边滤波(cv2.bilateralFilter)

前面我们介绍的滤波方法都会对图像造成模糊&#xff0c;使得边缘信息变弱或者消失&#xff0c;因此需要一种能够对图像边缘信息进行保留的滤波算法&#xff0c;双边滤波是综合考虑空间信息和色彩信息的滤波方式&#xff0c;在滤波过程中能够有效地保护 图像内的边缘信息。 6.1…

Bilateral Filters(双边滤波算法)原理及实现

双边滤波算法原理&#xff1a; 双边滤波是一种非线性滤波器&#xff0c;它可以达到保持边缘、降噪平滑的效果。和其他滤波原理一样&#xff0c;双边滤波也是采用加权平均的方法&#xff0c;用周边像素亮度值的加权平均代表某个像素的强度&#xff0c;所用的加权平均基于高斯分…

双边滤波的基本原理

双边滤波&#xff08;Bilateral filter&#xff09;是一种非线性的滤波方法&#xff0c;本质是基于高斯滤波&#xff0c;目的是解决高斯滤波造成的边缘模糊。结合图像的空间邻近度和像素值相似度的一种折处理&#xff0c;同时考虑空域信息和灰度相似性&#xff0c;达到保边去噪…

双边滤波算法原理

1. 简介 图像平滑是一个重要的操作&#xff0c;而且有多种成熟的算法。这里主要简单介绍一下Bilateral方法&#xff08;双边滤波&#xff09;&#xff0c;这主要是由于前段时间做了SSAO&#xff0c;需要用bilateral blur 算法进行降噪。Bilateral blur相对于传统的高斯blur来说…

Bilateral Filters(双边滤波算法)原理及实现(一)

双边滤波算法原理 双边滤波是一种非线性滤波器&#xff0c;它可以达到保持边缘、降噪平滑的效果。和其他滤波原理一样&#xff0c;双边滤波也是采用加权平均的方法&#xff0c;用周边像素亮度值的加权平均代表某个像素的强度&#xff0c;所用的加权平均基于高斯分布[1]。最重要…