我在 TextInput 中的 DoubleValidator 有一个奇怪的行为。它允许放置逗号“,”和点“.”。当我输入一些浮点数时的符号。我当前的区域设置是“en_US”,显然验证器应该限制输入逗号符号的可能性。
有一段简单的代码可以重现这种行为。
import QtQuick 2.9
import QtQuick.Window 2.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
TextInput {
id: inputField
anchors.centerIn: parent
validator: DoubleValidator { bottom: -Infinity;
top: Infinity;
decimals: 4;
notation: DoubleValidator.StandardNotation }
text: "0"
}
}
稍后在代码中我尝试从该字段获取数字
x = value ? Number.fromLocaleString(Qt.locale(), inputField.value)
如果我用逗号分隔符打印数字,我会合理地收到警告
警告:QML 错误:错误:区域设置:Number.fromLocaleString():格式无效
并且 x 保存浮点数的整数部分。
如果您能帮助我理解我做错了什么,我将不胜感激。我在这里的唯一目的是根据用户区域设置正确验证浮点数字。
这是正确的行为;您的情况下的逗号 (
,
) 是千位分隔符