检查 QValidator 的状态
首先,抱歉我的英语不好。
我正在尝试从用户那里获取IP。我正在使用 QRegExpValidator 来检查用户输入。验证器成功阻止不需要的字符。但我想知道当用户单击按钮时它是一个正确的IP。当然,我可以手动检查文本,但似乎有更好的方法,使用 QValidator 的状态枚举。 QValidator.Acceptable 是我需要检查的。但我不知道如何使用它
这是我需要使用的: http://www.riverbankcomputing.co.uk /static/Docs/PyQt4/html/qvalidator.html#State-enum
这是我尝试过的(从主程序中抽象):
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_()
期望的输出:
ip= 192.168.
False
ip= 192.168.2.1
True
但是这就是我得到的:
ip= 192.168.
False
ip= 192.168.2.1
False
检查 QValidator 状态的正确方法是什么?
First, sorry for my bad English.
I'm trying to get an IP from user. I'm using QRegExpValidator for checking user input. The validator blocks unwanted characters succesfully. But I want to learn it's a proper IP when user clicked the button. Of course I can check the text manually, but there seems a better way, using QValidator's state enum. QValidator.Acceptable is what I need to check. But I can't figure out 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(abstracted from 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_()
Desired output:
ip= 192.168.
False
ip= 192.168.2.1
True
But thats what I get:
ip= 192.168.
False
ip= 192.168.2.1
False
What is the proper way of checking QValidator's state?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在这里没有做正确的事。比较:
将枚举类型与其值之一进行比较 - 这将始终为
False
!请改用
validate
方法:那么
192.168.2.1
的结果是:validate
返回的元组的第一个元素是状态,即您可以与QValidator
的各种状态进行比较:对于
192.168.2.1
打印True
You're not doing the right thing here. The comparison:
Compares an enumeration type to one of its values - this will always be
False
!Use the
validate
method instead:Then the result for
192.168.2.1
is:The first element of the tuple returned by
validate
is the state, which you can compare to the various states ofQValidator
:Prints
True
for192.168.2.1