双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

请问如何限制QTextEdit只能输入数字,字母而不能输入中文字?

面向未来_ 面向未来_ 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请问如何限制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.
...全文

相关推荐

QTextEdit只接受数字输入工程完整源码

QTextEdit只接受数字输入工程完整源码,包括事件过滤器和重写键盘事件两种实现方式,其中重写键盘事件方式可作为提升控件用,多次使用

我是黄同学

Qt4.8触摸屏软键盘实现:点击输入框呼出与事件过滤器详解

虚拟键盘是嵌入式触摸屏设备人机交互的重要组成部分。在没有物理键盘的工业终端上,通过Qt事件过滤器捕获输入框点击,并利用无边框悬浮窗口实现软键盘的呼出与定位,是一种高效且低耦合的解决方案。该方案不依赖系统输入法,规避了QInputContext插件在X11与QWS下的兼容性问题,仅借助Qt自带的窗口管理即可完成字符回填与焦点保持。适用于Qt4.8及类似老版本Qt工程,尤其适合需要快速集成到现有代码中的场景。本文将围绕点击输入框自动呼出软键盘的完整链路,解析事件过滤、键盘定位、焦点控制、按键插入及部署常见问题,

weixin_33946020

汉子占两行的QTextEdit

自定义自己的QTextEdit类,实现汉字占两行,字母数字占一行的功能。

haltwang

Qt中QTextEdit高度自适应

当QTextEdit中输入的文字长度过长,QTextEdit就会出现滚动条。如果不想出现滚动条,而想QTextEdit的高度随着文字的增加或减少而改变。 //widget.h #ifndef WIDGET_H #define WIDGET_H #include #include #include #include class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); QTextEdit *textEdit; private slots:

weixin_38720756

QTextEdit 实现 QLineEdit 的 setPlaceholderText() 和 setMaxLength ()函数

QTextEdit 实现 QLineEdit 的 setPlaceholderText() 和 setMaxLength ()函数

Pafey
评论 3
收藏 已收藏
分享
返回顶部
面向未来_
其他开发语言 3,425 成员 15,624 内容