如何使用QDateTime :: fromString?

Foo*_*Bar 5 qt qdatetime

我现在,这个问题听起来很愚蠢,但是我无法解决它。最糟糕的例子:

QString time_format = "yyyy-MM-dd  HH:mm:ss";
QDateTime a = QDateTime::currentDateTime();
QString as = a.toString(time_format);

qDebug() << as; // print "2014-07-16  17:47:04"

QDateTime b;
b.fromString(as,time_format);
assert(b.isValid()); // fails
Run Code Online (Sandbox Code Playgroud)

我创建一个有效的QDatetime,使用它创建一个字符串(正确),然后尝试再次将其转换为QDatetime(使用相同的time_format-string)。但是突然之间,字符串无法解析。

有任何想法吗?

rat*_*eak 6

fromString是返回日期的静态函数;因此,您需要执行以下操作:

QDateTime b = QDateTime::fromString(as,time_format);
Run Code Online (Sandbox Code Playgroud)

在您的代码中b永远不会偏离其默认的初始化状态