Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

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

    General and Desktop
    3
    3
    18456
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Peppy last edited by

      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...

      1 Reply Last reply Reply Quote 0
      • Y
        yshurik last edited by

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

        1 Reply Last reply Reply Quote 0
        • H
          HuXiKa last edited by

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

          If you can find faults of spelling in the text above, you can keep them.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post