gpt4 book ai didi

c++ - 如何将整数变量转换为 QTime 对象

转载 作者:行者123 更新时间:2023-11-28 06:48:06 26 4
gpt4 key购买 nike

我对这个 QT 东西很陌生。这可能是一个愚蠢的问题,但我找不到任何答案。我浏览了 qt 文档并在互联网上搜索以找到这个问题的答案。

我的问题是,假设有一个“整数变量”,其持续时间值以秒为单位。我需要将其转换为 QTime 对象以作为 QTime 返回。我怎样才能在 Qt Creator 中做到这一点??..

int seekTime;
int seconds = QTime().secsTo(duration);
seekTime = seconds * bytePos/totalSize;
return seekTime;

我需要将此 seekTime 变量作为 QTime 对象返回,我该怎么做?

提前谢谢...!

最佳答案

这应该有效。

QTime t = QTime().addSecs(duration);

这是我试过的一个小程序:

#include <iostream>

#include <QTime>

int main()
{
int durationInSeconds = 40;
QTime t = QTime().addSecs(durationInSeconds);
std::cout << "h: " << t.hour() << ", m: " << t.minute() << " s: " << t.second() << ", ms: " << t.msec() << std::endl;
return 0;
}

这是我得到的输出:

h: 0, m: 0 s: 40, ms: 0

Update

A QTime can also be constructed to represent the seconds as:

int durationInSeconds = 40;
QTime t(0, 0, durationInSeconds);

更新 2

secsTo 函数可用于计算 QTime 的两个实例之间的秒差。这是文档:

int QTime::secsTo ( const QTime & t ) const

Returns the number of seconds from this time to t. If t is earlier than this time, the number of seconds returned is negative.

Because QTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400 and 86400.

secsTo() does not take into account any milliseconds.

假设你有:

QTime t1(0, 1, 0:
QTime t2(0, 0, 45);

int secs = t2.secsTo(t1); // secs should be equal to 15.
secs = t1.secsTo(t2); // secs should be equal to -15.

希望能稍微阐明QTime::secsTo 的预期行为。

关于c++ - 如何将整数变量转换为 QTime 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24566747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com