前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PyQt5 对话框 数据验证

PyQt5 对话框 数据验证

作者头像
用户6021899
发布2019-08-14 17:38:25
1.1K0
发布2019-08-14 17:38:25
举报

本篇介绍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_()

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-02-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档