您的位置:首页 > 编程语言 > Qt开发

pyqt4中对话框模态和非模态

2015-10-15 16:41 513 查看
这里的smart dialogs一般都是指的非模态对话框,就是用户想要实时的看到自己的操作对主窗口的影响,当然在某些模态对话框中,添加预览功能可以实现上面的需求,但是这些在非模态对话框中是很容易做到的。
我们在前面模态对话框的字体选择程序基础上加以修改。

#coding=utf-8

import sys

from PyQt4.QtCore import *

from PyQt4.QtGui import *

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)

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)

self.FontPropertiesDlg=None

self.format=dict(fontstyle=u"宋体",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"模态和非模态对话框")

self.updataData()

def updataData(self):

self.label.setText(u"选择的字体:%s

字体大小:%d

是否特效:%s" %(self.format["fontstyle"],self.format["fontsize"],self.format["fonteffect"]))

def FontModalDialog(self):

dialog = FontPropertiesDlg(self)

if dialog.exec_():

self.format["fontstyle"] = unicode(dialog.FontstyleComboBox.currentText())

self.format["fontsize"] = dialog.FontSizeSpinBox.value()

self.format["fonteffect"] = dialog.FontEffectCheckBox.isChecked()

self.updataData()

def FontModalessDialog(self):

dialog = FontPropertiesDlg(self)

dialog.setModal(False)

if dialog.show():

pass

app = QApplication(sys.argv)

font= MainDialog()

font.show()

app.exec_()

运行就可以实时改变用户的选择了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: