Qt正则表达式限制输入

中文匹配:

    QRegExp reg("^[u4e00-u9fa5]+$");
    QValidator*validator = new QRegExpValidator(reg);
    ui->lineEdit->setValidator(validator);

字母数字匹配:

    QRegExp reg("^[a-zA-Z0-9]+$");
    QValidator*validator = new QRegExpValidator(reg);
    ui->lineEdit->setValidator(validator);

IP、Port、Channel匹配:

QRegExp regExpIP("((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])[\.]){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])");
QRegExp regExpNetPort("((6553[0-5])|[655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{3}|[1-9][0-9]{2}|[1-9][0-9]|[0-9])");
QRegExp regExpChannel("[0-5]");

ui->lineEdit_IP->setValidator(new QRegExpValidator(regExpIP));
ui->lineEdit_NetPort->setValidator(new QRegExpValidator(regExpNetPort));
ui->lineEdit_Channel->setValidator(new QRegExpValidator(regExpChannel));

  

原文地址:https://www.cnblogs.com/wisdomroc/p/12123552.html