QRegExp reg("[a-zA-Z0-9]+$"); 新建表達式,括號中爲表達式
官方文檔給出的使用格式
// regexp: optional '-' followed by between 1 and 3 digits
QRegExp rx("-?\\d{1,3}");
QValidator *validator = new QRegExpValidator(rx, this);
QLineEdit *edit = new QLineEdit(this);
edit->setValidator(validator);
轉化成自己的代碼使用格式
QRegExp reg("[a-zA-Z0-9]+$");
QValidator *vali=new QRegExpValidator(reg,ui->user);
ui->user->setValidator(vali);
輸入這串代碼的位置
reg::reg(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::reg)
{
ui->setupUi(this);
this->setMaximumSize(338, 438);
QRegExp reg("[a-zA-Z0-9]+$");
QValidator *vali=new QRegExpValidator(reg,ui->user);
ui->user->setValidator(vali);
}
正則表達式可以百度查詢