好吧,这让我发疯了。我确定这是微不足道的,但是我一直在寻找答案并没有看到它。我敢肯定,这将是一个平坦的额头。
我正在用 Python 设计一个 Qt4 对话框。我通过 QDesigner 生成了代码,系统上有 4 个输入:
- QLineEdit(不能为空)
- QPlainText编辑
- QLineEdit(不能为空)
- QComboBox(需要选择其中一个选项)
问题:是否存在使字段为“必需”的标志?强制它是非空白的?
我试图使用 QRegExpValidator,但不确定这是正确的:
regex = QRegExp(r"\\S+")
self.optionName.setValidator(QRegExpValidator(regex,self))
我知道我遗漏了一些明显的东西(请不要让它成为 self.optionName.setRequired() 函数)。
更新
我现在添加了这个类:
from PyQt4 import QtGui
class ValidStringLength(QtGui.QValidator):
def __init__(self, min, max, parent):
QtGui.QValidator.__init__(self, parent)
self.min = min
self.max = max
def validate(self, s, pos):
if self.max > -1 and len(s) > self.max:
return (QValidator.Invalid, pos)
if self.min > -1 and len(s) < self.min:
return (QValidator.Intermediate, pos)
return (QValidator.Acceptable, pos)
def fixup(self, s):
pass
像这样称呼它:
self.optionName.setValidator(ValidStringLength(2, 8, self.optionName))
self.criteriaName.setValidator(ValidStringLength(2, 8, self.criteriaName))
并在类的 validate() 函数处设置断点,但从未调用过。
我错过了一些基本的东西吗?
TIA
麦克风