How would one set an input validator on a QLineEdit such that it restricts it to a valid IP address? i.e. x.x.x.x where x must be between 0 and 255.
Printable View
How would one set an input validator on a QLineEdit such that it restricts it to a valid IP address? i.e. x.x.x.x where x must be between 0 and 255.
Input masks are not sufficient for that. You need a validator. Refer to the wiki for details.
you need to use QRegExpValidator with a regexp accepting the IP address (I did it like this but not sure if it works perfectly :P):
Code:
QRegExp rx("((1{0,1}[0-9]{0,2}|2[0-4]{1,1}[0-9]{1,1}|25[0-5]{1,1})\\.){3,3}(1{0,1}[0-9]{0,2}|2[0-4]{1,1}[0-9]{1,1}|25[0-5]{1,1})"); v->setRegExp(rx); ui->lineEdit->setValidator(v);