QLineEdit控件只允许输入整数/浮点数

在MFC编程中,我们可以通过设置输入框的属性,让用户只能输入数字。

在QT中的输入框(QLineEdit)可以通过绑定QIntValidator/QDoubleValidator/QRegExpValidator对象来控制用户的输入。

***

QIntValidator           --  只让用户输入整数

QDoubleValidator     --  只让用户输入浮点数

QRegExpValidator    --  只让用户按照正则表达式定义好的样式进行输入

**

--- 以下为只让用户输入[-50,50]整数的示例 ---

QLineEdit* aEdit = new QLineEdit;
QIntValidator* aIntValidator = new QIntValidator;
aIntValidator->setRange(-50, 50);
aEdit->setValidator(aIntValidator);

QDoubleValidator和QRegExpValidator也是类似的。对于QRegExpValidator,关键是要构造出符合自己条件的正则表达式。



原文地址:https://www.cnblogs.com/kekec/p/2139817.html