Search code examples
qtipqlineeditqregexp

QLineEdit IP partial validation


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


Solution

  • 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));