PyQt编程之模态与非模态对话框(二)
生活随笔
收集整理的這篇文章主要介紹了
PyQt编程之模态与非模态对话框(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在上一篇里,實現的模態對話框的功能就是修改數據顯示的格式,并進行提交后驗證。在未應用該對話框之前,用戶不能與對話框的父窗口以及父窗口的兄弟窗口就行交互,這樣就保證了應用程序相關部分的狀態不會在該對話框關閉之前改變。
可是如果我們想并不確定這一次的設置效果如何,需要多次調整設置的時候,模態對話框就顯得不那么方便了。 這時候就可以利用非模態對話框,點擊“應用”(apply)按鈕來預覽設置修改后的結果。
一般來說,模態對話框含有“接受”(accept)按鈕和“拒絕”(reject)按鈕;非模態對話框則含有“應用”(apply)按鈕。
同樣的目的,這次是非模態對話框。
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_()框架講解 這里的框架和上一篇差不多,不必過多講解。
實現分析 1.這里有一句self.setAttribute(Qt.WA_DeleteOnClose)?,就是讓用戶點擊“close”的時候關閉對話框而不是隱藏對話框。
2.self.decimalMarkerEdit.setInputMask("X")??????設置輸入掩碼,推薦一篇博文,講解得很詳細。http://blog.csdn.net/xgbing/article/details/7776422
3.self.thousandsEdit.setValidator(QRegExpValidator(punctuationRe, self))??? 利用正則表達式驗證。
4.self.format =?? format;可以直接對對話框里面的格式字典進行改變而改變主窗口的格式字典。
與上一篇區別分析: 在這里結合預防驗證和提交后驗證,并利用正則表達式驗證器和輸入掩碼進行預防式驗證。效率得到提高。 能夠在用戶點擊“apply‘得到預覽效果,并和父窗口進行交互。
總結
以上是生活随笔為你收集整理的PyQt编程之模态与非模态对话框(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PyQt4编程之模态与非模态对话框(一)
- 下一篇: Python Qt GUI快速编程第六章