QDoubleValidator Class
QDoubleValidator 类提供浮点数的范围检查。更多
Header: | #include <QDoubleValidator> |
CMake.QDoubleValidator | 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 |
信号
void | bottomChanged(double bottom) |
void | decimalsChanged(int decimals) |
void | notationChanged(QDoubleValidator::Notation notation) |
void | topChanged(double top) |
详细说明
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
该枚举定义了输入 double 时允许使用的符号。
常量 | 值 | 说明 |
---|---|---|
QDoubleValidator::StandardNotation | 0 | 字符串按标准格式书写,整数部分后可选择加上分隔符和小数部分,例如"0.015" 。 |
QDoubleValidator::ScientificNotation | 1 | 字符串以科学形式书写,可在标准格式后附加指数部分,例如"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) |
Notifier 信号:
void | bottomChanged(double bottom) |
另请参见 setRange().
decimals : int
该属性用于保存验证器小数点后的最大位数。
默认情况下,该属性的值为-1,即接受任何位数。
访问功能:
int | decimals() const |
void | setDecimals(int) |
Notifier 信号:
void | decimalsChanged(int decimals) |
另请参见 setRange().
notation : Notation
该属性包含字符串描述数字的符号
默认情况下,此属性设置为ScientificNotation 。
访问功能:
QDoubleValidator::Notation | notation() const |
void | setNotation(QDoubleValidator::Notation) |
Notifier 信号:
void | notationChanged(QDoubleValidator::Notation notation) |
另请参阅 Notation 。
top : double
此属性保存验证器的最大可接受值
默认情况下,该属性的值为无穷大。
访问功能:
double | top() const |
void | setTop(double) |
Notifier 信号:
void | topChanged(double top) |
另请参阅 setRange().
成员函数文档
[explicit]
QDoubleValidator::QDoubleValidator(QObject *parent = nullptr)
使用parent 对象构造验证器对象,该对象可接受任何 double。
QDoubleValidator::QDoubleValidator(double bottom, double top, int decimals, QObject *parent = nullptr)
用parent 对象构造一个验证器对象。该验证器将接受从bottom 到top (含 )的双数,小数点后最多有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 不能表示双数或小数点后数字过多,则返回Invalid 。
注:如果有效范围内只有正二进制数(如 0.0 至 100.0),而input 是负二进制数,则返回Invalid 。如果notation() 设置为StandardNotation ,且输入值小数点前的位数多于有效范围内的双倍数,则返回Invalid 。如果notation() 设置为ScientificNotation ,而输入值不在有效范围内,则返回Intermediate 。通过改变指数,该值可能会变为有效值。
默认情况下,该验证器不使用pos 参数。
© 2025 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.