vdkotian
vdkotian

Reputation: 559

How to make IP validation for QtGui.QInputDialog.GetText()

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

Answers (1)

AlexVhr
AlexVhr

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

  1. Create QRegExp object, like this:

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) 
  1. Call 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

Related Questions