Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QLineEdit - Validator doesn't allow to input IP Address
Forum Update on Tuesday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 18.9k Views 1 Watching
  • 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 Offline
    P Offline
    Peppy
    wrote on 8 May 2011, 13:06 last edited by
    #1

    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
    0
    • Y Offline
      Y Offline
      yshurik
      wrote on 8 May 2011, 13:44 last edited by
      #2

      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
      0
      • H Offline
        H Offline
        HuXiKa
        wrote on 8 May 2011, 14:10 last edited by
        #3

        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
        0

        1/3

        8 May 2011, 13:06

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved