我们在前面模态对话框的字体选择程序基础上加以修改。先写一个类,ModelessDialog.py,如下:
#!/usr/bin/env python
#coding=utf-8
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class FontPropertiesDlg(QDialog):
def __init__(self,parent=None):
super(FontPropertiesDlg, self).__init__(parent)
self.format=format
FontStyleLabel = QLabel(u"中文字体:")
self.FontstyleComboBox = QComboBox()
self.FontstyleComboBox.addItems([u"宋体", u"黑体", u"仿宋",
u"隶书", u"楷体"])
self.FontEffectCheckBox =QCheckBox(u"使用特效")
FontSizeLabel = QLabel(u"字体大小")
self.FontSizeSpinBox = QSpinBox()
self.FontSizeSpinBox.setRange(1, 90)
applyButton = QPushButton(u"应用")
cancelButton = QPushButton(u"取消")
buttonLayout = QHBoxLayout()
buttonLayout.addStretch()
buttonLayout.addWidget(applyButton)
buttonLayout.addWidget(cancelButton)
layout = QGridLayout()
layout.addWidget(FontStyleLabel, 0, 0)
layout.addWidget(self.FontstyleComboBox, 0, 1)
layout.addWidget(FontSizeLabel, 1, 0)
layout.addWidget(self.FontSizeSpinBox, 1, 1)
layout.addWidget(self.FontEffectCheckBox,1,2)
layout.addLayout(buttonLayout, 2, 0)
self.setLayout(layout)
self.connect(applyButton,SIGNAL("clicked()"),self.apply)
self.connect(cancelButton,SIGNAL("clicked()"),self,SLOT("reject()"))
self.setWindowTitle(u"字体")
def apply(self):
self.format["fontstyle"] =unicode(self.FontstyleComboBox.currentText())
self.format["fontsize"] = self.FontSizeSpinBox.value()
self.format["fonteffect"] =self.FontEffectCheckBox.isChecked()
self.emit(SIGNAL("changed"))
编写主程序:
#!/usr/bin/env python
#coding=utf-8
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ModelessDialog
class FontPropertiesDlg(QDialog):
def __init__(self,parent=None):
super(FontPropertiesDlg, self).__init__(parent)
FontStyleLabel = QLabel(u"中文字体:")
self.FontstyleComboBox = QComboBox()
self.FontstyleComboBox.addItems([u"宋体",u"黑体", u"仿宋",
u"隶书", u"楷体"])
self.FontEffectCheckBox =QCheckBox(u"使用特效")
FontSizeLabel = QLabel(u"字体大小")
self.FontSizeSpinBox = QSpinBox()
self.FontSizeSpinBox.setRange(1, 90)
okButton = QPushButton(u"确定")
cancelButton = QPushButton(u"取消")
buttonLayout = QHBoxLayout()
buttonLayout.addStretch()
buttonLayout.addWidget(okButton)
buttonLayout.addWidget(cancelButton)
layout = QGridLayout()
layout.addWidget(FontStyleLabel, 0, 0)
layout.addWidget(self.FontstyleComboBox, 0, 1)
layout.addWidget(FontSizeLabel, 1, 0)
layout.addWidget(self.FontSizeSpinBox, 1, 1)
layout.addWidget(self.FontEffectCheckBox,1,2)
layout.addLayout(buttonLayout, 2, 0)
self.setLayout(layout)
#为okButton与cancelButton部件的点击信号添加槽函数
self.connect(okButton,SIGNAL("clicked()"),self,SLOT("accept()"))
self.connect(cancelButton,SIGNAL("clicked()"),self,SLOT("reject()"))
self.setWindowTitle(u"字体")
class MainDialog(QDialog):
def __init__(self,parent=None):
super(MainDialog,self).__init__(parent)
#创建一个FontPropertiesDlg对象
self.FontPropertiesDlg=None
#定义一个字典
self.format=dict(fontstyle="宋体",fontsize=1,fonteffect=False)
#创建两个按钮部件
FontButton1 = QPushButton(u"设置字体(模态)")
FontButton2 = QPushButton(u"设置字体(非模态)")
self.label = QLabel(u"默认选择")
#创建网格部件
layout = QGridLayout()
layout.addWidget(FontButton1,0,0)
layout.addWidget(FontButton2,0,1)
layout.addWidget(self.label)
self.setLayout(layout)
#信号与槽函数,槽函数为自身定义的函数
self.connect(FontButton1,SIGNAL("clicked()"),self.FontModalDialog)
self.connect(FontButton2,SIGNAL("clicked()"),self.FontModalessDialog)
self.setWindowTitle(u"模态与非模态对话框")
#实现updataData函数
self.updataData()
def updataData(self):
#为上面创建的label部件添加内容
self.label.setText(u"选择字体:%s
字体大小:%d
是否特效:%s"
%(self.format["fontstyle"],self.format["fontsize"],self.format["fonteffect"]))
def FontModalDialog(self):
#创建一个FontPropertiesDlg类对象并且赋值
dialog = FontPropertiesDlg(self)
if dialog.exec_():
self.format["fontstyle"] =dialog.FontstyleComboBox.currentText()
self.format["fontsize"] = dialog.FontSizeSpinBox.value()
self.format["fonteffect"] = dialog.FontEffectCheckBox.isChecked()
self.updataData()
print(self.format)
def FontModalessDialog(self):
dialog = ModelessDialog.FontPropertiesDlg(self)
self.connect(dialog,SIGNAL("changed"),self.updataData)
dialog.show()
app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
font= MainDialog()
font.show()
app.exec_()
运行结果如下:
1.self.emit(SIGNAL("changed"))
2.self.connect(dialog,SIGNAL("changed"),self.updataData)
可以看到只要用户点击“应用”,后程序发现填出对话框内容发生改变,则立刻产生消息“changed”,主程序消息循环得到这个消息,立刻运行updataData,所以主窗口更新了内容。