Qt ip和特殊输入字符输入框正则表达式

前端之家收集整理的这篇文章主要介绍了Qt ip和特殊输入字符输入框正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

正则表达式来实现ip地址,网络端口,信号通道的输入功能的,不符合规范的数据将不能输入:

QLineEdit的setValidator函数.

/* ip输入框正则表达式 */
// IP 前3段

 QRegExp regExp("[0-9][0-9.][0-9.][.]");
ui->lineEdit_1->setValidator(new QRegExpValidator(regExp,ui->lineEdit_1));
ui->lineEdit_2->setValidator(new QRegExpValidator(regExp,ui->lineEdit_2));
ui->lineEdit_3->setValidator(new QRegExpValidator(regExp,ui->lineEdit_3));
// 第4段
regExp = QRegExp("[0-9][0-9][0-9]");
ui->lineEdit_4->setValidator(new QRegExpValidator(regExp,ui->lineEdit_4));
// 端口输入框
regExp = QRegExp("[0-9][0-9][0-9][0-9][0-9]");
ui->lineEdit_6->setValidator(new QRegExpValidator(regExp,ui->lineEdit_6));
ui->lineEdit_7->setValidator(new QRegExpValidator(regExp,ui->lineEdit_7));
// 输入处理
if (arg1.toInt() > 255) {
        QMessageBox::information(this,"information","输入有误");
        ui->lineEdit_1->setText(tr(""));
}
// 如果输入.号
if ((arg1.indexOf(QString("."))) != -1) {
        ui->lineEdit_1->setText(arg1.section('.',0));
        focusNextPrevChild(true);
}

正则表达式网络配置通用代码

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_locIP->setValidator(new QRegExpValidator(regExpIP,this));  
ui->lineEdit_subNetMask->setValidator(new QRegExpValidator(regExpIP,this));  
ui->lineEdit_GatewayIP->setValidator(new QRegExpValidator(regExpIP,this));  
ui->lineEdit_serIP->setValidator(new QRegExpValidator(regExpIP,this));  
ui->lineEdit_serPort->setValidator(new QRegExpValidator(regExpNetPort,this));  
ui->lineEdit_rfChannel->setValidator(new QRegExpValidator(regExpChannel,this)
原文链接:https://www.f2er.com/regex/358301.html

猜你在找的正则表达式相关文章