请问如何限制QTextEdit只能输入数字,字母而不能输入中文字?
面向未来_ 2010-07-22 12:11:26 请问如何限制QTextEdit只能输入数字,字母而不能输入中文字?网上查了下也是
QIntValidator, QDoubleValidator, and QRegExpValidator
QValidator *validator = new QIntValidator(100, 999, this);
QLineEdit *edit = new QLineEdit(this);
// the edit lineedit will only accept integers between 100 and 999
edit->setValidator(validator);
这里只能限制数字,如果有更多的限制呢?
关于QRegExpValidator,我这里有个例子:
1 QRegExpValidator *validator;
2 validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this);//有两个构造函数,QRegExpValidator ( QObject * parent )和
//QRegExpValidator ( const QRegExp & rx, QObject * parent ),这
//里用的是第2个
3 validator->validate(text, pos);
****************************************************************************************
QValidator的枚举类型:
enum QValidator::State
This enum type defines the states in which a validated string can exist.
QValidator::Invalid 0 The string is clearly invalid.
QValidator::Intermediate 1 The string is a plausible intermediate value during editing.
QValidator::Acceptable 2 The string is acceptable as a final result; i.e. it is valid.
QValidator::State QRegExpValidator::validate ( QString & input, int & pos ) const [virtual]这是一个有着枚举类型返回值的虚函数。
Returns Acceptable if input is matched by the regular expression for this validator, Intermediate if it has matched partially (i.e. could be a valid match if additional valid characters are added), and Invalid if input is not matched.
The pos parameter is set to the length of the input parameter.
pos参数用来设置输入参数的长度。
For example, if the regular expression is \w\d\d (word-character, digit, digit) then "A57" is Acceptable, "E5" is Intermediate, and "+9" is Invalid.
Reimplemented from QValidator.