【问题标题】:QDateTime Not Properly Setting With Different ConstructorsQDateTime 没有正确设置不同的构造函数
【发布时间】:2012-01-24 05:34:05
【问题描述】:

我有一个返回 QDateTime 类的函数,代码如下:

QDateTime Foo:IntToQDateTime( int Date )
{
    int Second = 4,
        Minute = 3,
        Hour   = 22,
        Day    = 10,
        Month  = 11,
        Year   = 2011;
    QDate d(Year, Month, Day);
    QTime t(Hour, Minute, Second);
    QDateTime r(d, t);
    return r;
}

这会产生空字符串/time_t 为 4294967295 的 r,但 dt 都是准确的。

如果我将代码更改为:

QDateTime Foo:IntToQDateTime( int Date )
{
    int Second = 4,
        Minute = 3,
        Hour   = 22,
        Day    = 10,
        Month  = 11,
        Year   = 2011;
    QDate d(Year, Month, Day);    // November 11 2011
    QTime t(Hour, Minute, Second);// 22:03:04:00
    QDateTime r(QDate(Year, Month, Day), QTime(Hour, Minute, Second));
    return r;
}

r 现在是“Fri Nov 8 00:56:47 16182”,time_t 为 4294967295(同上)。谁能向我解释为什么a.) QDateTime 类r 的日期/时间不准确,以及为什么b.) 在构造函数中将dtQDate(...), QTime(...) 并列传递也会影响日期/时间。

【问题讨论】:

    标签: macos qt


    【解决方案1】:

    您是如何确定您报告的值的?

    有了这个程序:

    #include <QCoreApplication>
    #include <QDebug>
    #include <QDate>
    #include <QTime>
    
    QDateTime IntToQDateTime( int Date )
    {
        int Second = 4,
            Minute = 3,
            Hour   = 22,
            Day    = 10,
            Month  = 11,
            Year   = 2011;
        QDate d(Year, Month, Day);
        QTime t(Hour, Minute, Second);
        QDateTime r(d, t);
        return r;
    }
    
    int main(int argc, char** argv)
    {
      QCoreApplication a(argc, argv);
      QDateTime t = IntToQDateTime(0);
      qDebug() << t.toTime_t();
      qDebug() << t.toString();
      return 0;
    }
    

    我使用 Qt 4-8.0、MSVC-2010-Express、德语 Windows7/64 得到以下输出:

    1320958984
    "Do 10. Nov 22:03:04 2011"
    

    你确定你的环境是正确的并且你没有看到一些调试的假象吗?

    【讨论】:

    • 我相信你是对的。我发誓昨晚它甚至在输出上都不能正常工作,但现在它可以了!看来这是调试的产物。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多