PyQt编程之模态与非模态对话框(二)
生活随笔
收集整理的這篇文章主要介紹了
PyQt编程之模态与非模态对话框(二)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在上一篇里,實(shí)現(xiàn)的模態(tài)對(duì)話(huà)框的功能就是修改數(shù)據(jù)顯示的格式,并進(jìn)行提交后驗(yàn)證。在未應(yīng)用該對(duì)話(huà)框之前,用戶(hù)不能與對(duì)話(huà)框的父窗口以及父窗口的兄弟窗口就行交互,這樣就保證了應(yīng)用程序相關(guān)部分的狀態(tài)不會(huì)在該對(duì)話(huà)框關(guān)閉之前改變。
可是如果我們想并不確定這一次的設(shè)置效果如何,需要多次調(diào)整設(shè)置的時(shí)候,模態(tài)對(duì)話(huà)框就顯得不那么方便了。 這時(shí)候就可以利用非模態(tài)對(duì)話(huà)框,點(diǎn)擊“應(yīng)用”(apply)按鈕來(lái)預(yù)覽設(shè)置修改后的結(jié)果。
一般來(lái)說(shuō),模態(tài)對(duì)話(huà)框含有“接受”(accept)按鈕和“拒絕”(reject)按鈕;非模態(tài)對(duì)話(huà)框則含有“應(yīng)用”(apply)按鈕。
同樣的目的,這次是非模態(tài)對(duì)話(huà)框。
import sys from PyQt4.QtGui import * from PyQt4.QtCore import *class MainWindow(QWidget):def __init__(self,parent=None):QWidget.__init__(self,parent)button = QPushButton("click me",self)button.clicked.connect(self.setNumberFormat)self.format = dict(thousandsseparator=',',decimalmarker='.',decimalplaces=2,rednegatives=True)def setNumberFormat(self):dialog = Intelligent(self.format,self)self.connect(dialog,SIGNAL('changed'),self.refreshTable)dialog.show()def refreshTable(self):print 1class Intelligent(QDialog):def __init__(self,format,parent=None):super(Intelligent,self).__init__(parent)self.setAttribute(Qt.WA_DeleteOnClose) # ensure the dialog box can be detele while you click the 'close'punctuationRe = QRegExp(r"[ ,;:.]")thousandsLabel = QLabel("&Thousands separator")self.thousandsEdit = QLineEdit(format["thousandsseparator"])thousandsLabel.setBuddy(self.thousandsEdit)self.thousandsEdit.setMaxLength(1) #the new function setMaxLengthself.thousandsEdit.setValidator(QRegExpValidator(punctuationRe, self)) #set the checkdecimalMarkerLabel = QLabel("Decimal &marker")self.decimalMarkerEdit = QLineEdit(format["decimalmarker"])decimalMarkerLabel.setBuddy(self.decimalMarkerEdit)self.decimalMarkerEdit.setMaxLength(1)self.decimalMarkerEdit.setValidator(QRegExpValidator(punctuationRe, self))self.decimalMarkerEdit.setInputMask("X") # the new function ,set the input,and the X means anythingdecimalPlacesLabel = QLabel("&Decimal places")self.decimalPlacesSpinBox = QSpinBox()decimalPlacesLabel.setBuddy(self.decimalPlacesSpinBox)self.decimalPlacesSpinBox.setRange(0,6)self.decimalPlacesSpinBox.setValue(format['decimalplaces'])self.redNegativesCheckBox = QCheckBox("&Red negative numbers")self.redNegativesCheckBox.setChecked(format["rednegatives"])buttonBox = QDialogButtonBox(QDialogButtonBox.Apply|QDialogButtonBox.Close|QDialogButtonBox.Ok)self.format = formatlayout = QGridLayout()layout.addWidget(thousandsLabel,0,0)layout.addWidget(self.thousandsEdit,0,1)layout.addWidget(decimalMarkerLabel,1,0)layout.addWidget(self.decimalMarkerEdit,1,1)layout.addWidget(decimalPlacesLabel,2,0)layout.addWidget(self.decimalPlacesSpinBox,2,1)layout.addWidget(self.redNegativesCheckBox,3,0,1,2)layout.addWidget(buttonBox,4,0,1,2)self.setLayout(layout)self.connect(buttonBox.button(QDialogButtonBox.Apply),SIGNAL("clicked()"),self.apply) self.connect(buttonBox,SIGNAL("rejected()"),self,SLOT("reject()"))self.connect(buttonBox,SIGNAL("accepted()"),self,SLOT("accept()"))self.setWindowTitle("Set Number Format (Mode;ess)")def apply(self):thousands = unicode(self.thousandsEdit.text())decimal = unicode(self.decimalMarkerEdit.text())if thousands == decimal:QMessageBox.warning(self,"Format Error", "The thousands separator and the decimal marker""must be different.")self.thousandsEdit.selectAll()self.thousandsEdit.setFocus()returnif len(decimal) == 0:QMessageBox.warning(self,"Format Error","the decimal marker may nor be empty.")self.decimalMarkerEdit.selectAll()self.decimalMarkerEdit.setFocus()returnself.format['thousandsseparator'] = thousandsself.format['decimalmarker'] = decimalself.format['decimalplaces'] = \self.decimalPlacesSpinBox.value()self.format['rednegatives'] = \self.redNegativesCheckBox.isChecked()self.emit(SIGNAL("changed"))app = QApplication(sys.argv) widget = MainWindow() widget.show() app.exec_()框架講解 這里的框架和上一篇差不多,不必過(guò)多講解。
實(shí)現(xiàn)分析 1.這里有一句self.setAttribute(Qt.WA_DeleteOnClose)?,就是讓用戶(hù)點(diǎn)擊“close”的時(shí)候關(guān)閉對(duì)話(huà)框而不是隱藏對(duì)話(huà)框。
2.self.decimalMarkerEdit.setInputMask("X")??????設(shè)置輸入掩碼,推薦一篇博文,講解得很詳細(xì)。http://blog.csdn.net/xgbing/article/details/7776422
3.self.thousandsEdit.setValidator(QRegExpValidator(punctuationRe, self))??? 利用正則表達(dá)式驗(yàn)證。
4.self.format =?? format;可以直接對(duì)對(duì)話(huà)框里面的格式字典進(jìn)行改變而改變主窗口的格式字典。
與上一篇區(qū)別分析: 在這里結(jié)合預(yù)防驗(yàn)證和提交后驗(yàn)證,并利用正則表達(dá)式驗(yàn)證器和輸入掩碼進(jìn)行預(yù)防式驗(yàn)證。效率得到提高。 能夠在用戶(hù)點(diǎn)擊“apply‘得到預(yù)覽效果,并和父窗口進(jìn)行交互。
總結(jié)
以上是生活随笔為你收集整理的PyQt编程之模态与非模态对话框(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PyQt4编程之模态与非模态对话框(一)
- 下一篇: PyQt4编程之简短地做出多个选择框