QT正则表达式的使用——限制输入

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);
}

正则表达式可以百度查询

发布了14 篇原创文章 · 获赞 42 · 访问量 5883

猜你喜欢

转载自blog.csdn.net/qq_42451456/article/details/102600603