您的位置:首页 > 编程语言 > Qt开发

pyqt4文档阅读(10.1):QIntValidator-QDoubleValidator-QRegExpValidator

2016-10-01 18:14 351 查看
本系列文章长期更新修改.

QIntValidator,QDoubleValidator,QRegExpValidator,都是继承QValidator的验证器.

QIntValidator和QDoubleValidator分别是整数和浮点数的验证器,而QRegExpValidator则是正则表达式验证器,几乎可实现任何格式的输入验证.

属性:

QIntValidator

Methods

__init__ (self, QObject parent)
__init__ (self, int bottom, int top, QObject parent)
int bottom (self)
setBottom (self, int)
setRange (self, int bottom, int top)
setTop (self, int)
int top (self)
(QValidator.State, int) validate (self, QString, int)

QDoubleValidator

Types

enum Notation { StandardNotation, ScientificNotation }

Methods

__init__ (self, QObject parent)
__init__ (self, float bottom, float top, int decimals, QObject parent)
float bottom (self)
int decimals (self)
Notation notation (self)
setBottom (self, float)
setDecimals (self, int)
setNotation (self, Notation)
setRange (self, float bottom, float top, int decimals = 0)
setTop (self, float)
float top (self)
(QValidator.State, int) validate (self, QString, int)

QRegExpValidator

Methods

__init__ (self, QObject parent)
__init__ (self, QRegExp rx, QObject parent)
QRegExp regExp (self)
setRegExp (self, QRegExp rx)
(QValidator.State, int pos) validate (self, QString input, int pos)

详细分析:

1.validate()函数

三种验证器都有一个函数签名一样的validate()函数,该函数的分析参考QValidator篇.

(QValidator.State, int pos) validate (self, QString input, int pos)

2.基础验证属性

三种验证器有用于验证的基础属性.

其中QIntValidator和QDoubleValidator类似,都有一个上界和下界(闭区间),输入要在上下界之间.但QDoubleValidator多了个小数点位数decimals.

QDoubleValidator在逻辑上使用定点数作为输入.假如小数点位数设置为2,而你输入2.333,QDoubleValidator不会接受这个输入.

而QRegExpValidator则有一个正则表达式,输入要匹配这个表达式.

这些属性都可以在初始化的时候设置,或者之后用get/set函数.

QIntValidator
__init__ (self, QObject parent)
__init__ (self, int bottom, int top, QObject parent)

int bottom (self)
setBottom (self, int)

int top (self)

setTop (self, int)

setRange (self, int bottom, int top)

QDoubleValidator
__init__ (self, QObject parent)
__init__ (self, float bottom, float top, int decimals, QObject parent)

float bottom (self)

setBottom (self, float)

float top (self)

setTop (self, float)

setRange (self, float bottom, float top, int decimals = 0)

int decimals (self)

setDecimals (self, int)

QRegExpValidator
__init__ (self, QObject parent)
__init__ (self, QRegExp rx, QObject parent)

QRegExp regExp (self)
setRegExp (self, QRegExp rx)

3.QDoubleValidator的符号类型notation

QDoubleValidator有一个属性notation,会影响validate()函数的结果.

枚举量含义
QDoubleValidator.StandardNotation0标准浮点数(如0.015)
QDoubleValidator.ScientificNotation1科学计数法(如1.5e-2)
notation是一个枚举量,默认是QDoubleValidator.ScientificNotation

Notation notation (self)
setNotation (self, Notation)

当notation是标准型的时候,QDoubleValidator只接受0.015这样的输入,不接受1.5e-2.
而当它是科学计数法的时候,QDoubleValidator无论是0.015或1.5e-2都接受,这里要注意.

4.Intermediate和Invalid

不同验证器对QValidator的两个枚举量Intermediate和Invalid的理解是不同的.

对QIntValidator来说,如果数位太少,则返回Intermediate,数位太多或数字太大则返回Invalid.

比如,range是[100,499],输入50返回Intermediate,输入500返回Invalid.

对QDoubleValidator来说,range要考虑,小数点位数也要考虑.

在允许输入科学计数法的时候,当一个输入的小数点位数超出decimals属性时,如果这个数是标准型,则QDoubleValidator返回Invalid,如果是科学计数法的,则返回Intermediate.

对QRegExpValidator来说,如果输入再补充一些字符就能匹配的话,返回Intermediate,否则返回Invalid.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息