[Solved]QLineEdit - Validator doesn't allow to input IP Address



  • I've got this code:
    @
    ServerIpAddressEdit->setInputMask(QString("000.000.000.000; "));
    ServerIpAddressEdit->setValidator( new QRegExpValidator( QRegExp("[0-255].[0-255].[0-255].[0-255]" , Qt::CaseInsensitive), this));
    ServerIpPortEdit->setValidator(new QIntValidator(1, 65535, this));
    @

    QRegExpValidator doesn't allow me to input digits into ServerIpAddressEdit(QLineEdit), why?? When I comment this line, everything is okay, but it doesn't validate...



  • In RegExp: \. or even \\. instead just "."
    Also [0-255] means different, you may need \d{1,3}, to match 1-3 digits



  • I don't think you can mix masks and validators for this problem (after 5 long minutes of google search....).

    @QString Octet = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
    lineEdit->setValidator(new QRegExpValidator(
    QRegExp("^" + Octet + "\." + Octet + "\." + Octet + "\." + Octet + "$"), this));@

    This will work, stoled it from "here":http://lists.trolltech.com/qt-interest/2004-05/thread00085-0.html.

    edit:
    Or you could do something like "this":http://www.qtcentre.org/threads/6228-Ip-Address-Validation:

    @ class IP4Validator : public QValidator {
    public:
    IP4Validator(QObject *parent=0) : QValidator(parent){}
    void fixup(QString &input) const {}
    State validate(QString &input, int &pos) const {
    if(input.isEmpty()) return Acceptable;
    QStringList slist = input.split(".");
    int s = slist.size();
    if(s>4) return Invalid;
    bool emptyGroup = false;
    for(int i=0;i<s;i++){
    bool ok;
    if(slist[i].isEmpty()){
    emptyGroup = true;
    continue;
    }
    int val = slist[i].toInt(&ok);
    if(!ok || val<0 || val>255) return Invalid;
    }
    if(s<4 || emptyGroup) return Intermediate;
    return Acceptable;
    }
    };@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.