此小结学习日期、时间、定时器、时间编辑类的相关函数。
一、QDate
查看帮助文档可见:
QDATE类提供日期函数:QDATE对象包含公历中的日历日期,即年、月和日数。它可以从系统时钟读取当前日期。它提供了比较日期和操作日期的功能。例如,有可能添加和减去日期、月份和年份,具体如下:
返回值类型 | 函数名 | 说明 |
---|---|---|
QDate(int y, int m, int d) | 日期类对象通过指定的年月日实现如: QDate date(2025,1,1) | |
QDate | addDays(qint64 ndays) const | 添加n天之后的日期,n可以为负数 |
QDate | addMonths(int nmonths) const | 添加n月之后的日期,n可以为负数 |
QDate | addYears(int nyears) const | 添加n年之后的日期,n可以为负数 |
qint64 | daysTo(const QDate & d) const | 返回两个日期的间隔天数;小日期在前,否则结果为负数 |
int | day() const | 返回日期为该月的第几天; 返回0:日期出错 |
int | dayOfWeek() | const返回日期为该星期的第几天(星期几); 返回0:日期出错 |
int | dayOfYear() const | 返回日期为该年的第几天; 返回0:日期出错 |
int | daysInMonth() | const返回该月的天数 28-31; 返回0:日期出错 |
int | daysInYear() const | 返回该年的天数 365/366; 返回0:日期出错 |
QDate | 静态成员函数类可直接调用:static currentDate() | 获取当前的日期 |
QDate | static fromJulianDay(qint64 jd) | 从JulianDay的日期转为QDate |
QDate | static fromString(const QString & string, const QString & format) | 从字符串按照指定规则生成QDate |
bool | static isLeapYear(int year) | 判断是否为闰年 |
bool | static isValid() const | 判断日期是否正确 |
bool | static isValid(int year, int month, int day) | 给定年月日,判断日期是否正确 |
查看帮助文档可见:
QDate d1(1995, 5, 17); // May 17, 1995
QDate d2(1995, 5, 20); // May 20, 1995
d2.daysTo(d1); // returns -3
d1.daysTo(d2); // returns 3
QDate::fromString("20000110", "yyyyMMdd"); // January 10, 2000
QDate::fromString("1.30", "M.d"); // January 30 1900
QDate::isValid(2002, 5, 17); // true
QDate::fromString("20000110", "yyyyMd"); // January 10, 2000
QDate::isValid(2000, 2, 29); // true (2000 is a leap year)
QDate::isValid(2002, 2, 30); // false (Feb 30 does not exist)
QDate::isValid(2004, 2, 29); // true (2004 is a leap year)
QDate::isValid(2100, 2, 29); // false (2100 is not a leap year)
QDate::isValid(2006, 2, 29); // false (2006 is not a leap year)
QDate::isValid(1202, 6, 6); // true (even though 1202 is pre-Gregorian)
QDate显示格式
表达式 | 说明 |
d | 天数用 (1到 31)表示 |
dd | 天数用(01到31)表示 |
ddd | 天数用英文简写表示 ( 'Mon' to 'Sun') |
dddd | 天数用英文全写表示( 'Monday' to 'Sunday') |
M | 月数用 (1到 12)表示 |
MM | 月数用(01到12)表示 |
MMM | 月数用英文简写表示 ('Jan' to 'Dec') |
MMMM | 月数用英文全写表示 ( 'January' to 'December') |
yy | 年数用 (00 to 99)表示 |
yyyy | 年数用四位数表示。如果年份为负数,则减去负号。 |
查看帮助文档可见:
Format | Result |
dd.MM.yyyy | 20.07.1969 |
ddd MMMM d yy | Sun July 20 69 |
'The day is' dddd | The day is Sunday |
二、QTime
查看帮助文档可见:
QTIME类提供时钟时间函数。QTIME对象包含时钟时间,即从午夜开始的小时数、分钟数、秒数和毫秒数。它可以从系统时钟读取当前时间并测量经过的时间跨度。它提供了用于比较时间和通过添加毫秒来操纵时间的功能。QTIME使用24小时时钟格式,它没有AM/PM的概念。与QDateTime不同的是,QTime没有时区或夏令时概念。
返回值类型 | 函数名 | 说明 |
| QTime() | 构造一个时间为0的对象 |
| QTime(int h, int m, int s = 0, int ms = 0) | 构造一个具有初始时间的对象 |
QTime | addMSecs(int ms) const | 在当前时间基础上增加ms毫秒,ms可为负 |
QTime | addSecs(int s) const | 在当前时间基础上增加s秒,s可为负 |
int | hour() const | 返回小时数 |
int | minute() const | 返回分钟数 |
int | second() const | 返回秒 |
int | msec() const | 返回毫秒 |
int | elapsed() const | 计算与最近一次呼叫start()或者restart()函数间隔的毫秒数,相当于计时器 |
int | restart() | 将当前系统时间记录为当前时间,并返回距离上次呼叫start()或者restart()函数间隔的毫秒数 |
int | msecsTo(const QTime & t) const | 计算距离时间t的毫秒数,如果t早于当前时间,则为负 |
int | secsTo(const QTime & t) const | 计算距离时间t的秒数 |
bool | setHMS(int h, int m, int s, int ms = 0) | 设置标准HMS时间,如果不符合标准,返回false |
QString | toString(const QString & format) const | 将时间转化为字符串格式 |
void | start() | 将当前系统时间记录为当前时间 |
bool | static isValid(int h, int m, int s, int ms = 0) | 判断输入的时间是否有效 |
bool | static isValid() const | 判断当前对象的时间是否有效 |
QString | static toString(Qt::DateFormat format = Qt::TextDate) const | 按照Qt::DateFormat的格式转化 |
QTime | static fromString(const QString & string, Qt::DateFormat format = Qt::TextDate) | 从Qt::DateFormat转化为QTime对象 |
QTime | static fromString(const QString & string, const QString & format) | 从字符串格式转化为QTime对象 |
QTime | static currentTime() | 得到当前的系统时间 |
Expression | Output |
h | the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
m | the minute without a leading zero (0 to 59) |
mm | the minute with a leading zero (00 to 59) |
s | the second without a leading zero (0 to 59) |
ss | the second with a leading zero (00 to 59) |
z | the milliseconds without leading zeroes (0 to 999) |
zzz | the milliseconds with leading zeroes (000 to 999) |
AP | 使用 AM/PM时间 |
ap | 使用 AM/PM时间 |
QTime time(18,8,9,30);//设置时间为18时8分9秒30毫秒
QString time1 = time.toString("hh:mm:ss.zzz");//结果为18:08:09.030
QString time2 = time.toString("h:m:s.z");//结果为18:8:9.30
QString time3 = time.toString("h:m:s.z AP");//结果为6:8:9.30 PM
QString time4 = time.toString("h:m:s.z ap");//结果为6:8:9.30 pm
QString QTime::toString(Qt::DateFormat f = Qt::TextDate) const
//按照参数format指定的格式用字符串形式输出当前时间对象的时间.
//参数的可选值:
Qt::TextDate:格式为HH:MM:SS
Qt::ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SS
Qt::LocalDate:字符串格式依赖系统本地设置
QTime QTime::fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
//使用参数format指定的格式根据参数string指定的时间返回一个时间对象。如果string指定的时间不合法,则返回一个无效的时间对象。
//format可选值:
Qt::TextDate:格式为HH:MM:SS
Qt::ISODate:遵循ISO8601的时间表示格式,同样也为HH:MM:SS
Qt::LocalDate:字符串格式依赖系统本地设置
三、QDateTime
顾名思义,QDateTime是类QDate和类QTime的相结合,它可以从系统时钟中读取当前日期时间。它提供比较日期时间和操作日期时间的函数,比如加上一定数量的秒、天、月或年。
和QDate相比可以设置时区属性:setTimeZone(const QTimeZone &toZone)
四、QTimer
QTimer是一个计时器类
它的使用分三步,创建对象,连接signal和slot函数,start()
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));//系统调用update函数重绘界面,也可以写自定义的槽函数
timer->start(1000);
其中,SIGNAL(timeout())表示:每当计时结束,计时器归零并重新计时,并发送一个信号激活slot函数。
而 timer->start(1000);当中的1000,就是1000毫秒的意思,表示每次timeout的时间间隔是1000ms
如果我们想让这个计时器只计时一次,那么必须使用void setSingleShot(bool singleShot)函数。
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->setsetSingleShot(true)
timer->start(60000);
这样计时器只会倒计时1分钟,然后结束。
当然我们还可以改变计时周期
void setInterval(int msec) QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
五、QDateEdit、QTimeEdit、QDateTimeEdit
QDateEdit和QTimeEdit均继承自QDateTimeEdit,许多特性和功能都有QDateTimeEdit提供。这些都是相关属性,它们用来编辑日期和时间:
如QDateEdit主要一下函数:
(1) date:保存了部件的显示日期。
(2) minimumDate:定义了用户可以设置的最小日期。
(3) maximumDate:定义了用户可以设置的最大日期。
(4) displayFormat:包含了一个字符串用于格式化日期。