qt QLineEdit的几种输入限制,备自己查阅

    QRegExp rx("[a-zA-Z0-9][a-zA-Z0-9.-]+$");/*限制第一位只能输a-zA-Z0-9,之后只能输入a-zA-Z0-9.-这几类字符*/

    QValidator *vali=new QRegExpValidator(rx,ui.lineEdit_host1);
    ui.lineEdit_host1->setValidator(vali);
    ui.lineEdit_host2->setValidator(vali);
    ui.lineEdit_host1->setMaxLength(128);//限制输入长度,最大为128
    ui.lineEdit_host2->setMaxLength(128);
    QRegExp regx("[0-9]+$");//只能输入数字
    QValidator *validator=new QRegExpValidator(regx,ui.lineEdit_port1);
    ui.lineEdit_port1->setValidator(new QIntValidator(0,65535,ui.lineEdit_port1));
    ui.lineEdit_port2->setValidator(new QIntValidator(0,65535,ui.lineEdit_port2));
    ui.lineEdit_port1->setMaxLength(5);
    ui.lineEdit_port2->setMaxLength(5);
    ui.lineEdit_HttpPort->setValidator(new QIntValidator(0,65535,ui.lineEdit_HttpPort));
    ui.lineEdit_HttpPort->setMaxLength(5);
    ui.lineEdit_ProxyPort1->setMaxLength(5);
    QRegExp regx2("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");//IP格式正则表达式,只能按照IP地址格式输入。
    QValidator *validator2=new QRegExpValidator(regx2,ui.lineEdit_HttpHost);
    ui.lineEdit_HttpHost->setValidator(validator2);    
    ui.lineEdit_Dns1->setValidator(validator2);

你可能感兴趣的:(qt,输入限制)