答案 0 :(得分:1)
QML中没有等效项。但是,您可以使用TextField和自定义验证器轻松创建自己的小部件:
在QML中:
TextField {
text : "01/01/1970 00:00:00"
inputMask: "99/99/9999 99:99:99"
validator: DateTimeValidator {}
}
在C ++中:
// datetimevalidator.h
#include <QValidator>
#include <QDateTime>
class DateTimeValidator: public QValidator
{
public:
DateTimeValidator();
State validate(QString& input, int& pos) const;
};
// datetimevalidator.cpp
#include <datetimevalidator.h>
DateTimeValidator::DateTimeValidator(): QValidator()
{}
QValidator::State DateTimeValidator::validate(QString& input, int& pos) const
{
QDateTime dt = QDateTime::fromString(input, "MM/dd/yyyy HH:mm:ss");
if (dt.isNull()) // If null, the input cannot be parsed
{
return QValidator::Invalid;
}
return QValidator::Acceptable;
}
在main()中,注册您的验证器以在QML中使用它:
#include "datetimevalidator.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
qmlRegisterType<DateTimeValidator>("my.components", 1, 0, "DateTimeValidator");
...
}