添加头文件
#include "QRegExp"
#include "QRegExpValidator"
关键代码
QRegExp regx("^[4-9]|[1-9][0-9]|[1-3][0-4][0-9]|350$");
QValidator *validator = new QRegExpValidator(regx,ui->lineEdit);
ui->lineEdit->setValidator(validator);
常用正则表达式,有些是没验证的
//1-100的正整数
QRegExp regExp1("^1|[1-9][0-9]?|100$");
//浮点数,不限制小数点位数
QRegExp regx2("^(-?\\d+)(\\.\\d+)?$");
//浮点数,限制小数点位数
QRegExp regx3("^(-?\\d+)(\\.\\d{1,2})?$");
//由数字和26个英文字母组成的字符串
QRegExp regx3("^[A-Za-z0-9]+$");
//由26个英文字母组成的字符串
QRegExp regx4("^[A-Za-z]+$");
//由26个英文字母的大写组成的字符串
QRegExp regx5("^[A-Z]+$");
//由26个英文字母的小写组成的字符串
QRegExp regx6("^[a-z]+$");
//由26个英文字母的小写组成的字符串
QRegExp regx7("^[a-z]+$");
//由数字、26个英文字母或者下划线组成的字符串
QRegExp regx8("^\\w+$");
//非负浮点数(正浮点数 + 0)
QRegExp regx9("^\\d+(\\.\\d+)?$");
//正浮点数
QRegExp regx10("^0|([1-9]+(\\.[0-9]{1,2})?)$");
//非正浮点数(负浮点数 + 0)
QRegExp regx11("^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$");
//整数
QRegExp regx12("^-?\\d+$");
//负整数
QRegExp regx13("^-[0-9]*[1-9][0-9]*$");
//非正整数(负整数 + 0)
QRegExp regx14("^((-\\d+)|(0+))$");
//正整数
QRegExp regx15("^[0-9]*[1-9][0-9]*$");
//非负整数
QRegExp regx16("^\\d+$");
参考博客: