本篇介绍PyQt5对话框的数据合法性的验证。有两种验证方式:预防式验证(preventative)和 提交后验证 (post-mortem)。预防式验证适合于单个部件的数据验证,可以通过选择合适的部件类型,设置最大字符长度或者数字的大小范围,应用正则表达式等等来进行预防式验证。提交后验证可应用于单个控件,也可应用于多部件交叉验证。两种验证策略可单独或者组合使用。
具体的运用请参见如下代码:
import sys from PyQt5.QtCore import Qt, pyqtSignal from PyQt5.Qt import QRegExp,QRegExpValidator from PyQt5.QtWidgets import *
#非模态对话框("Apply"型更新),自定义信号,数据验证,正则表达式,掩码
class NumberFormatDlg(QDialog): changed = pyqtSignal()#自定义信号 def __init__(self,format_,parent=None): super().__init__(parent) thousandsLabel = QLabel("&Thousands seprator") self.thousandsEdit = QLineEdit(format_["thousandsseparator"]) self.thousandsEdit.setMaxLength(1)
#正则表达式 punctuationRe = QRegExp(r"[ ,;:.]") #只能为中括号内的某个字符,还可以为空 #设定验证方式为正则表达式,不合规的字符输入不进去。预防式验证 self.thousandsEdit.setValidator(QRegExpValidator(punctuationRe,self)) thousandsLabel.setBuddy(self.thousandsEdit)
decimalMarkerLabel = QLabel("Decimal &marker") self.decimalMarkerEdit = QLineEdit(format_["decimalmarker"]) self.decimalMarkerEdit.setMaxLength(1) self.decimalMarkerEdit.setValidator(QRegExpValidator(punctuationRe,self)) self.decimalMarkerEdit.setInputMask("X")#设定掩码 decimalMarkerLabel.setBuddy(self.decimalMarkerEdit)
decimalPlacesLabel = QLabel("&Decimal places") self.decimalPlacesSpinBox = QSpinBox() self.decimalPlacesSpinBox.setRange(0,6) self.decimalPlacesSpinBox.setValue(format_["decimalplaces"])
decimalPlacesLabel.setBuddy(self.decimalPlacesSpinBox)
self.redNegativesCheckBox = QCheckBox("&Red negative numbers") self.redNegativesCheckBox.setChecked(format_["rednegatives"])
buttonBox = QDialogButtonBox(QDialogButtonBox.Apply | QDialogButtonBox.Close)
buttonBox.button(QDialogButtonBox.Apply).clicked.connect(self.apply) buttonBox.rejected.connect(self.reject)
grid = QGridLayout() grid.addWidget(thousandsLabel,0,0) grid.addWidget(self.thousandsEdit,0,1) grid.addWidget(decimalMarkerLabel,1,0) grid.addWidget(self.decimalMarkerEdit,1,1) grid.addWidget(decimalPlacesLabel,2,0) grid.addWidget(self.decimalPlacesSpinBox,2,1) grid.addWidget(self.redNegativesCheckBox,3,0,1,2) grid.addWidget(buttonBox,4,0,1,2) self.setLayout(grid)
self.setWindowTitle("数字格式设置(非模态)")
self.format = format_
def apply(self): thousands = self.thousandsEdit.text() decimal = self.decimalMarkerEdit.text()
#提交后验证 #交叉验证 if thousands == decimal: QMessageBox.warning(self,"格式错误","千位分隔符和小数点符不能相同") self.thousandsEdit.selectAll() self.thousandsEdit.setFocus() return if decimal == '': QMessageBox.warning(self,"格式错误","小数点符不能为空") self.decimalMarkerEdit.selectAll() self.decimalMarkerEdit.setFocus() return
self.format["thousandsseparator"] = thousands self.format["decimalmarker"] = decimal self.format["decimalplaces"] = self.decimalPlacesSpinBox.value() self.format["rednegatives"] = self.redNegativesCheckBox.isChecked() self.changed.emit()#发射自定义信号
class MainDialog(QDialog): def __init__(self,parent=None): super(MainDialog,self).__init__(parent) format_ = dict(thousandsseparator =",",decimalmarker=".",decimalplaces = 3,rednegatives =True) self.dialog = NumberFormatDlg(format_) button = QPushButton("click here",self) button.clicked.connect(self.dialog.show) app = QApplication(sys.argv) font= MainDialog() font.show() app.exec_()