经常用到限制输入,总结一下:
- 只能输入数字;
QRegExp regExp2("^[0-9]*[1-9][0-9]*$");
- 只能输入英文,数字;
QRegExp regExpNum("[a-zA-Z0-9]{1,}");
QRegExpValidator *pattern = new QRegExpValidator(regExpNum, this);
ui->lineEdit->setValidator(pattern);
- 只能输入正整数并限输入范围(0~199)//年龄
QRegExp regExp("(^1$)|(^1[0-9]\\d$)|(^[1-9]\\d$)|(^[1-9]$)|^0$"); //("^[1-9][0-9]{1,3}$"); //^[1-9][0-9]*$ 任意位数正整数
ui->lineEdit->setValidator(new QRegExpValidator(regExp, this));
4.只能输入0~12; //月;
QRegExp regExpMonth("^([0-9]|1[0-2])$");
ui->lineEdit->setValidator(new QRegExpValidator(regExpMonth, this));
5. 只能输入1~31; //日;
QRegExp regExpday("^(([1-9])|((1|2)[0-9])|30|31)$");
ui->lineEdit->setValidator(new QRegExpValidator(regExpday, this));