gpt4 book ai didi

python - PyQt QWizard 验证和按钮覆盖

转载 作者:太空宇宙 更新时间:2023-11-04 01:10:50 24 4
gpt4 key购买 nike

我有一个多页 QWizard,我需要在其中对数字输入进行一些验证。多个 QLineEdit 小部件可以包含任何浮点类型或字符串“无”,其中“无”是 sqlite 中 REAL 列的默认空值。 QValidator 可以验证 float 部分,但它在您键入时进行验证,因此不适合评估“无”字符串(例如,用户可以输入 NNNooo)。对每个 QLineEdit 失去焦点的验证也不合适,因为用户可能不会在移动到下一页之前选择每个 QLE。我能想到的就是通过覆盖/拦截下一个按钮调用来验证所有字段。在 QWizard 页面中,我可以断开下一个按钮(无法使用新样式断开连接):

self.disconnect(self.button(QWizard.NextButton), QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('next()'))
self.button(QWizard.NextButton).clicked.connect(self.validateOnNext)

在初始化的 QWizardPages 中,我可以连接到下一个按钮(新样式):

self.parent().button(QWizard.NextButton).clicked.connect(self.nextButtonClicked) 

但断开 QWizard 的下一个插槽不起作用(2 种方式):

self.parent().button(QWizard.NextButton).clicked.disconnect(self.next) 

我得到一个 AttributeError: 'MyWizardPage' object has no attribute 'next'

self.parent().disconnect(self.parent().button(QWizard.NextButton), QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('next()'))

我没有收到任何错误,但下一步按钮仍然有效

每个 QWizardPage 连接到“下一个”插槽的问题是每个页面中的 init 方法都在向导启动期间执行 - 因此当按下 next 时,所有向导页面的 nextButtonClicked() 方法都会被执行。也许我可以禁用 QWizardPage onFocus() 上的所有下一个功能,实现它自己的下一个功能,并对每个页面执行相同的操作,但似乎过于复杂

什么是一个简单的验证问题现在是一个信号/插槽拦截器问题。有什么想法吗?

最佳答案

您可以轻松地创建自己的接受自定义值的验证器子类。您需要做的就是重新实现它的 validate方法。

这是一个使用 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_())

关于python - PyQt QWizard 验证和按钮覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27731913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com