First off, sorry for my bad english.
I am trying to get an IP address from a user. I use QRegExpValidator to validate user input. The validator successfully blocks unwanted characters. But I want to know that this is the correct IP address when the user clicked the button. Of course, I can check the text manually, but it seems to be the best way using the QValidator state enumeration. QValidator.Acceptable is what I need to check. But I canβt understand how I can use it.
Here is what I need to use: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qvalidator.html#State-enum
And here is what I tried (abstracts from the main program):
from PyQt4 import QtCore, QtGui from functools import partial class Gui(QtGui.QDialog): def __init__(self): QtGui.QDialog.__init__(self) editLayout=QtGui.QFormLayout() edit=QtGui.QLineEdit() edit.setMinimumWidth(125) regex=QtCore.QRegExp("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") validator=QtGui.QRegExpValidator(regex, edit) edit.setValidator(validator) editLayout.addRow("Enter Client IP:", edit) button=QtGui.QPushButton("Add Client") button.clicked.connect(partial(self.addClientButtonClicked, edit, validator)) layout=QtGui.QVBoxLayout() layout.addLayout(editLayout) layout.addWidget(button) self.setLayout(layout) def addClientButtonClicked(self, edit, validator): print("ip=", edit.text()) print(validator.State==QtGui.QValidator.Intermediate) app=QtGui.QApplication([]) g=Gui() g.show() app.exec_()
Required Conclusion:
ip= 192.168. False ip= 192.168.2.1 True
But here is what I get:
ip= 192.168. False ip= 192.168.2.1 False
What is the correct way to check the status of QValidator?
source share