请问如何限制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.
...全文
2655 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuguangzhou123 2011-12-31
  • 打赏
  • 举报
回复
求知道。。。
cheng20100915 2010-08-02
  • 打赏
  • 举报
回复
帮你顶帖
james2222 2010-07-27
  • 打赏
  • 举报
回复
顶一顶..

3,423

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧