1、输入整数部分
lineEdit->setValidator(new QIntValidator(1, 1000, this)); //但是仍然能输入0000000
2、简单的小数输入
lineEdit->setValidator(new QDoubleValidator(-180.0,180.0,6,this));
3、使用正则表达式输入
1)输入(0,1) 两位小数
QRegExp rx3("1|([0-0]{1}[\.][0-9]{1,2})");
QRegExpValidator *pReg3 = new QRegExpValidator(rx3, this);
lineEdit3->setValidator(pReg3);
2)输入(0.1,10) 一位小数
QRegExp rx("^?(10|[0-0]?\\d(\\.\\d{1})?)$");
QRegExpValidator *pReg = new QRegExpValidator(rx, this); lineEdit1->setValidator(pReg);
4、限制输入的为数字和字母
QRegExp regx("[a-zA-Z0-9]+$"); QValidator *validator = new QRegExpValidator(regx, lined ); lined->setValidator( validator );
参考博文:
1)Qt QLineEdit 限制输入类型以及大小Int 和double http://blog.csdn.net/a_sungirl/article/details/17373405
2)限制QLineEdit的数值输入范围 http://blog.csdn.net/giselite/article/details/12708031
3)限制QLineEdit的数值输入范围 http://qimo601.iteye.com/blog/1262395