我有一个多页QWizard,在这里我需要对数字输入进行一些验证。多个QLineEdit小部件可以包含任何浮点类型或字符串'None',其中'None‘是sqlite中实际列的默认空值。QValidator可以验证浮点部分,但是当它在您输入时验证时,它不适合计算'None‘字符串(例如用户可以输入NNNooo )。对于每个QLineEdit上焦点丢失的验证也不合适,因为用户在进入下一页之前可能不会选择每个QLE。我所能想到的就是通过重写/拦截next按钮调用来验证所有字段。在QWizard页面中,我可以断开next按钮(无法使新样式断开工作):
self.disconnect(self.button(QWizard.NextButton), QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('next()'))
self.button(QWizard.NextButton).clicked.connect(self.validateOnNext)在init内部的QWizardPages中,我可以连接到next按钮(新样式):
self.parent().button(QWizard.NextButton).clicked.connect(self.nextButtonClicked) 但是,断开QWizard的下一个插槽不起作用(2种方法):
self.parent().button(QWizard.NextButton).clicked.disconnect(self.next) 我得到一个AttributeError:'MyWizardPage‘对象没有属性'next’
self.parent().disconnect(self.parent().button(QWizard.NextButton), QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('next()'))我没有错误,但next按钮仍然有效。
每个QWizardPage连接到“next”插槽的问题是,每个页面中的init方法在向导启动时执行,所以当按下next时,所有向导页面nextButtonClicked()方法都会执行。也许我可以在QWizardPage onFocus()上禁用所有下一个功能,实现它自己的下一个功能,并对每个页面执行相同的操作,但是看起来过于复杂了。
一个简单的验证问题现在是信号/时隙拦截器问题。有什么想法吗?
发布于 2015-01-01 19:03:16
您可以轻松地创建自己的验证器子类,该子类将接受自定义值。您所需要做的就是重新实现它的验证方法。
下面是一个使用QDoubleValidator的简单示例
from PyQt4 import QtCore, QtGui
class Validator(QtGui.QDoubleValidator):
def validate(self, value, pos):
text = value.strip().title()
for null in ('None', 'Null', 'Nothing'):
if text == null:
return QtGui.QValidator.Acceptable, text, pos
if null.startswith(text):
return QtGui.QValidator.Intermediate, text, pos
return super(Validator, self).validate(value, pos)
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.edit = QtGui.QLineEdit(self)
self.edit.setValidator(Validator(self.edit))
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.edit)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 200, 50)
window.show()
sys.exit(app.exec_())https://stackoverflow.com/questions/27731913
复制相似问题