Qt :QTime类的使用

The QTime class provides clock time functions.

A QTime object contains a clock time, i.e. the number of hours, minutes, seconds, and milliseconds since midnight. It can read the current time from the system clock and measure a span of elapsed time. It provides functions for comparing times and for manipulating a time by adding a number of milliseconds.

QTime uses the 24-hour clock format; it has no concept of AM/PM. Unlike QDateTime, QTime knows nothing about time zones or daylight-saving time (DST).

A QTime object is typically created either by giving the number of hours, minutes, seconds, and milliseconds explicitly, or by using the static function currentTime(), which creates a QTime object that contains the system's local time. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.

The hour(), minute(), second(), and msec() functions provide access to the number of hours, minutes, seconds, and milliseconds of the time. The same information is provided in textual format by the toString() function.

QTime provides a full set of operators to compare two QTime objects. QTime A is considered smaller than QTime B if A is earlier than B.

The addSecs() and addMSecs() functions provide the time a given number of seconds or milliseconds later than a given time. Correspondingly, the number of seconds or milliseconds between two times can be found using secsTo() or msecsTo().

QTime can be used to measure a span of elapsed time using the start(), restart(), and elapsed() functions.

See also QDate and QDateTime

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
#include <QDebug>
#include <windows.h>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTime time = QTime::currentTime();//获取当前事件
    QString strTime = time.toString("hh:mm:ss.zzz");//转换成QString
    time = QTime::fromString("02:23:45.789","hh:mm:ss.zzz");//QString转QTime
    strTime = time.toString("hh:mm:ss.zzz");
    QTime time1(7,30,5 ,100);//传递时分秒毫秒构造函数
    strTime = time1.toString("hh:mm:ss.zzz");
    int hour = time1.hour();//获取小时,7
    qDebug()<<hour;
    int minute=time1.minute();//获取分钟,30
    qDebug()<<minute;
    int second = time1.second();//获取秒数,5
    qDebug()<<second;
    int mssecond = time1.msec();//获取毫秒,100
    qDebug()<<mssecond;
    QTime t1 = time1.addMSecs(100);//增加100ms
    qDebug()<<t1.msec();
    QTime t2 = time1.addSecs(80);//增加80s
    qDebug()<<t2.second();
    if(time < time1)//比较两个QTime,时间晚的大
        qDebug()<<"time<time1";
    QTime t3;
    t3.start();//开始计时
    Sleep(1000);
    int elspsed = t3.elapsed();//计算多少ms过去了
    t3.restart();//重新计时
    int milsecond = QTime::currentTime().msecsSinceStartOfDay();//返回从零点开始共计多少ms
    qDebug()<<milsecond;
    QTime t4(22,25,10);
    QTime t5(22,24,0);
    qDebug()<<t4.secsTo(t5);//两者相差了多少秒,t4到t5需要多少秒,如果t5<t4,返回负值
    qDebug()<<strTime;
}

MainWindow::~MainWindow()
{
    delete ui;
}

.

Qt是一个跨平台的C++应用程序开发框架,它提供了丰富的库和工具,可以用于开发图形界面应用程序、网络应用程序、数据库应用程序等。QTimeQt中的一个,用于处理时间相关的操作。 QTime提供了一些方法来获取和操作时间,包括获取当前时间、设置时间、计算时间差等。下面是一些常用的QTime的方法: 1. `QTime::currentTime()`:获取当前时间。 2. `QTime::setHMS(int hour, int minute, int second)`:设置时间的小时、分钟和秒。 3. `QTime::hour()`、`QTime::minute()`、`QTime::second()`:获取时间的小时、分钟和秒。 4. `QTime::addSecs(int seconds)`:增加指定的秒数。 5. `QTime::secsTo(const QTime &time)`:计算当前时间与指定时间之间的秒数差。 6. `QTime::toString(const QString &format)`:将时间转换为字符串,可以指定格式。 以下是一个使用QTime的示例代码: ```cpp #include <QCoreApplication> #include <QDebug> #include <QTime> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 获取当前时间 QTime currentTime = QTime::currentTime(); qDebug() << "Current time: " << currentTime.toString("hh:mm:ss"); // 设置时间为12:30:45 QTime customTime; customTime.setHMS(12, 30, 45); qDebug() << "Custom time: " << customTime.toString("hh:mm:ss"); // 增加10秒 customTime = customTime.addSecs(10); qDebug() << "Custom time after adding 10 seconds: " << customTime.toString("hh:mm:ss"); // 计算时间差 int secondsDiff = currentTime.secsTo(customTime); qDebug() << "Seconds difference: " << secondsDiff; return a.exec(); } ``` 运行以上代码,你将会看到输出的当前时间、设置的自定义时间、增加秒数后的时间以及时间差。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值