Important: Please read the Qt Code of Conduct -

QLineEdit::setValidator(const QValidator*) validates the QLineEdit::displayText() not QLineEdit::text()

  • Let's say you want to create a QLineEdit that allows the user to enter an IPv4/6 address.


    QLineEdit* ipAddressEditor = new QLineEdit;
    ipAddressEditor->setInputMask( ";_" ); // IP address; blanks are _ - the example is taken from Qt library 4.8's "QLineEdit documentation":
    ipAddressEditor->setValidator(new IpAddressValidator( ipAddressEditor ) );


    class IpAddressValidator : public QValidator
    explicit IpAddressValidator( QObject* parent = 0 );

    virtual State validate( QString& input, int& pos ) const { ... }



    The input mask ";_" is taken from Qt library 4.8's "QLineEdit documentation":

    QLineEdit gives its displayText member function value ("127.__0.__0.__1" for instance) as input parameter to the IpAddressValidator instead of the value of its text member function ("").

    The question:

    Shouldn't there be another property of QLineEdit that allows the client of QLineEdit to explicitly specify which property - displayText or text should be taken in consideration by the validator?

    Should a feature request/bug report be filed for this case?

Log in to reply