Reputation: 559
I am writing code in PySide which has a button "Change IP". When the button is clicked a dialog box appears which has text box. I want validation on the text box which only to accept IP address.
I am using this code:
QtGui.QInputDialog.getText(self, "Title", "Enter IP: ")
Upvotes: 1
Views: 1601
Reputation: 2074
AFAIR, QInputDialog.getText
does not support on-the-fly validation, but if you are willing to roll your own dialog, you could use QRegExpValidator
1.Look up the needed regex on the Internets (here, for instance). See that it looks like
^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
for IP4
rx = QRegExp("regular_expression_string_from_step_1")
3.Create QRegExpValidator
instance and pass the rx
object to it's constructor, like this:
my_validator = QRegExpValidator(rx)
my_line_edit.setValidator(my_validator)
That's it, my_line_edit
should now refuse entry of non-valid IP adresses. If you don't want to go this way, you could just use python's own re moodule for post-factum validation using the regex from step 1.
Upvotes: 5