本页内容

QDoubleValidator Class

QDoubleValidator 类提供了对浮点数的范围检查功能。更多内容...

头文件: #include <QDoubleValidator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承自: QValidator

公共类型

enum Notation { StandardNotation, ScientificNotation }

属性

公共函数

QDoubleValidator(QObject *parent = nullptr)
QDoubleValidator(double bottom, double top, int decimals, QObject *parent = nullptr)
virtual ~QDoubleValidator()
double bottom() const
int decimals() const
QDoubleValidator::Notation notation() const
void setBottom(double)
void setDecimals(int)
void setNotation(QDoubleValidator::Notation)
void setRange(double minimum, double maximum, int decimals)
void setRange(double minimum, double maximum)
void setTop(double)
double top() const

重新实现的公共函数

(since 6.3) virtual void fixup(QString &input) const override
virtual QValidator::State validate(QString &input, int &pos) const override

详细说明

QDoubleValidator 提供了上界、下界以及小数点后位数的限制。

您可以通过一次调用setRange()来设置可接受的范围,或者通过setBottom()和setTop()来设置。使用setDecimals()设置小数位数。validate()函数返回验证状态。

QDoubleValidator 使用其locale() 方法来解析数字。例如,在德语区域设置中,“1,234”将被视为小数 1.234。在阿拉伯语区域设置中,QDoubleValidator 将接受阿拉伯数字。

注意: 在locale()中设置的 QLocale::NumberOptions 也会影响数字的解析方式。例如,由于默认情况下未设置QLocale::RejectGroupSeparator ("C" 区域设置除外),验证器将接受分组分隔符。如果字符串通过验证,请将其传递给locale().toDouble() 以获取其数值。

另请参阅 QIntValidator 、QRegularExpressionValidator 、QLocale::toDouble() 以及“行编辑”示例。

成员类型文档

enum QDoubleValidator::Notation

此枚举定义了输入双精度数值时允许的表示形式。

常量值描述
QDoubleValidator::StandardNotation0字符串采用标准格式书写,即整数部分后可选地跟一个分隔符,然后是小数部分,例如"0.015" 。
QDoubleValidator::ScientificNotation1字符串采用科学记数法书写,即在标准格式后可选地附加一个指数部分,例如"1.5E-2" 。

整数部分可以像往常一样包含正负号。 这与小数部分、指数以及任何数字分组的分隔符一样,都取决于区域设置。QDoubleValidator 不会检查它找到的任何数字分组分隔符的位置(这也取决于区域设置),但如果在locale().numberOptions() 中设置了QLocale::RejectGroupSeparator ,它将拒绝包含这些分隔符的输入。

另请参阅 QLocale::numberOptions()、QLocale::decimalPoint()、QLocale::exponential() 以及QLocale::negativeSign()。

属性文档

bottom : double

该属性存储了验证器的最小可接受值

默认情况下,此属性的值为 -infinity。

访问函数:

double bottom() const
void setBottom(double)

另请参阅 setRange()。

decimals : int

该属性用于指定验证器允许的小数点后最大位数

默认情况下,该属性的值为 -1,这意味着允许任意数量的小数位。

访问函数:

int decimals() const
void setDecimals(int)

另请参阅 setRange()。

notation : Notation

该属性指定字符串如何表示一个数字的规则

默认情况下,该属性设置为ScientificNotation 。

访问函数:

QDoubleValidator::Notation notation() const
void setNotation(QDoubleValidator::Notation)

另请参阅 Notation 。

top : double

该属性存储了验证器可接受的最大值

默认情况下,该属性包含无穷大值。

访问函数:

double top() const
void setTop(double)

另请参阅 setRange()。

成员函数文档

[explicit] QDoubleValidator::QDoubleValidator(QObject *parent = nullptr)

使用一个parent 对象构建一个验证器对象,该对象接受任何double类型的值。

QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals, QObject *parent = nullptr)

使用parent 对象创建一个验证器对象。该验证器将接受从bottom 到top (含)范围内的double类型数值,小数点后最多可有decimals 位。

[virtual noexcept] QDoubleValidator::~QDoubleValidator()

销毁该验证器。

[override virtual, since 6.3] void QDoubleValidator::fixup(QString &input) const

重写了:QValidator::fixup(QString &input) const。

尝试将字符串 `input ` 转换为 `Acceptable ` 格式的 `double` 数值。

该数字的格式由notation()、decimals()、locale()以及后者的numberOptions()决定。

为了符合notation() 的规范,当使用ScientificNotation 时,固定值将以归一化形式表示,这意味着任何非零值在小数点前都将有一个非零位。

QString input = "0.98765e2";
QDoubleValidator val;
val.setLocale(QLocale::C);
val.setNotation(QDoubleValidator::ScientificNotation);
val.fixup(input); // input == "9.8765e+01"

为了符合decimals() 的规范,当-1 时,使用的位数将由QLocale::FloatingPointShortest 决定。否则,如果数字的小数部分长度超过decimals(),则会截断(并酌情进行四舍五入)。当notation() 为ScientificNotation 时,此操作将在数字转换为归一化形式之后进行。

input = "-1234.6789";
val.setDecimals(2);
val.setLocale(QLocale::C);
val.setNotation(QDoubleValidator::StandardNotation);
val.fixup(input); // input == "-1234.68"

注意:若 decimals()被设置为大于std::numeric_limits<double>::digits10 ,且字符串提供的位数超过该值,则小数部分中超出该数量的位数可能会被更改。当解析为double 时,生成的字符串应编码相同的浮点数。

该函数首次出现在 Qt 6.3 中。

void QDoubleValidator::setRange(double minimum, double maximum, int decimals)

将验证器设置为接受范围在minimum 到maximum (含)之间的双精度数,小数点后最多decimals 位。

注意:将 小数位数设置为 -1 实际上等同于设置为无限制。这也是默认构造的验证器所使用的值。

void QDoubleValidator::setRange(double minimum, double maximum)

将验证器设置为接受从minimum 到maximum (含)范围内的双精度数值,且不改变小数点后位数。

这是一个重载函数。

[override virtual] QValidator::State QDoubleValidator::validate(QString &input, int &pos) const

重写:QValidator::validate(QString &input, int &pos) const。

如果字符串input 格式正确且包含一个在有效范围内的 double 值,则返回Acceptable 。

如果input 格式错误或包含超出范围的 double 值,则返回Intermediate 。

如果input 不表示一个 double 值,或者小数点后位数过多,则返回Invalid 。

注意:如果有效范围仅包含正双精度数值(例如 0.0 到 100.0),而input 为负双精度数值,则返回Invalid 。如果notation() 设置为StandardNotation ,且输入的小数点前位数超过有效范围内的双精度数值可能拥有的最大位数,则返回Invalid 。 如果notation()的值为ScientificNotation ,且输入值不在有效范围内,则返回Intermediate 。通过改变指数,该值仍可能变为有效值。

默认情况下,此验证器不使用pos 参数。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.