2

In my project I want to filter some of my data via IP input.

I also want to allow to filter by partial IP input for example : 192.168.

I found out how to set the complete IP validation.

  QString oIpRange;
    QRegExpValidator *poIpValidator;

    // Client IP validation mask
    oIpRange =
            "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
    QRegExp oIpRegex ("^" + oIpRange
                     + "\\." + oIpRange
                     + "\\." + oIpRange
                     + "\\." + oIpRange + "$");
    poIpValidator =
            new QRegExpValidator(oIpRegex,
                                 poQtLineEdit);

    // Client IP set validator
    poQtLineEdit->setValidator( poIpValidator );

I connect the QLineEdit "returnPressed" signal to my filter function.

The problem is that the "returnPressed" signal only emitted when I enter the complete IP and not for partial.

Any suggestion how to fix that issue ?

Thanks

Simon
  • 1,522
  • 2
  • 12
  • 24

3 Answers3

3

You can use QRegExpValidator to do that.

// #include <QRegExpValidator>
QString oIpRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
QRegExp oIpRegex ("^" + oIpRange
              + "\\." + oIpRange
              + "\\." + oIpRange
              + "\\." + oIpRange + "$");
ed->setValidator(new QRegExpValidator(oIpRegex));
Ziming Song
  • 1,176
  • 1
  • 9
  • 22
0

For partial validation:

oIpRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
QRegExp oIpRegex ("^" + oIpRange
                 + "\\." + oIpRange
                 + "(\\." + oIpRange + ")?"
                 + "(\\." + oIpRange + ")?$");
ahmed
  • 5,430
  • 1
  • 20
  • 36
0

Thanks for your answer I add a minor fix to the regular expression

QString oIpRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
QRegExp oIpRegex ("^" + oIpRange
                         + "(\\." + oIpRange + ")?"
                         + "(\\." + oIpRange + ")?"
                         + "(\\." + oIpRange + ")?$");
Simon
  • 1,522
  • 2
  • 12
  • 24