在实际的程序开发中,经常会用到各种各样的消息框来给用户一些提示或提醒,Qt提供了QMessageBox类来实现此项功能。在本实例中,分析了各种消息框的使用方式及之间的区别。各种消息框的使用如图所示:
实现代码如下:
- #
-*- coding: utf-8 -*- - from
PyQt4.QtGui import * - from
PyQt4.QtCore import * - import
sys -
- QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
-
- class
MessageBoxDlg(QDialog): -
def __init__(self,parent=None): -
super(MessageBoxDlg,self).__init__(parent) -
self.setWindowTitle("Messagebox") -
self.label=QLabel("About Qt MessageBox") -
questionButton=QPushButton("Question") -
informationButton=QPushButton("Information") -
warningButton=QPushButton("Warning") -
criticalButton=QPushButton("Critical") -
aboutButton=QPushButton("About") -
aboutqtButton=QPushButton("About Qt") -
customButton=QPushButton("Custom") -
-
gridLayout=QGridLayout(self) -
gridLayout.addWidget(self.label,0,0,1,2) -
gridLayout.addWidget(questionButton,1,0) -
gridLayout.addWidget(informationButton,1,1) -
gridLayout.addWidget(warningButton,2,0) -
gridLayout.addWidget(criticalButton,2,1) -
gridLayout.addWidget(aboutButton,3,0) -
gridLayout.addWidget(aboutqtButton,3,1) -
gridLayout.addWidget(customButton,4,0) -
-
self.connect(questionButton,SIGNAL("clicked()"),self.slotQuestion) -
self.connect(informationButton,SIGNAL("clicked()"),self.slotInformation) -
self.connect(warningButton,SIGNAL("clicked()"),self.slotWarning) -
self.connect(criticalButton,SIGNAL("clicked()"),self.slotCritical) -
self.connect(aboutButton,SIGNAL("clicked()"),self.slotAbout) -
self.connect(aboutqtButton,SIGNAL("clicked()"),self.slotAboutQt) -
self.connect(customButton,SIGNAL("clicked()"),self.slotCustom) -
-
def slotQuestion(self): -
button=QMessageBox.question(self,"Question", -
self.tr("已到达文档结尾,是否从头查找?"), -
QMessageBox.Ok|QMessageBox.Cancel, -
QMessageBox.Ok) -
if button==QMessageBox.Ok: -
self.label.setText("Question button/Ok") -
elif button==QMessageBox.Cancel: -
self.label.setText("Question button/Cancel") -
else: -
return -
-
def slotInformation(self): -
QMessageBox.information(self,"Information", -
self.tr("填写任意想告诉于用户的信息!")) -
self.label.setText("Information MessageBox") -
-
def slotWarning(self): -
button=QMessageBox.warning(self,"Warning", -
self.tr("是否保存对文档的修改?"), -
QMessageBox.Save|QMessageBox.Discard|QMessageBox.Cancel, -
QMessageBox.Save) -
if button==QMessageBox.Save: -
self.label.setText("Warning button/Save") -
elif button==QMessageBox.Discard: -
self.label.setText("Warning button/Discard") -
elif button==QMessageBox.Cancel: -
self.label.setText("Warning button/Cancel") -
else: -
return -
-
def slotCritical(self): -
QMessageBox.critical(self,"Critical", -
self.tr("提醒用户一个致命的错误!")) -
self.label.setText("Critical MessageBox") -
-
def slotAbout(self): -
QMessageBox.about(self,"About",self.tr("About事例")) -
self.label.setText("About MessageBox") -
-
def slotAboutQt(self): -
QMessageBox.aboutQt(self,"About Qt") -
self.label.setText("About Qt MessageBox") -
-
def slotCustom(self): -
customMsgBox=QMessageBox(self) -
customMsgBox.setWindowTitle("Custom message box") -
lockButton=customMsgBox.addButton(self.tr("锁定"), -
QMessageBox.ActionRole) -
unlockButton=customMsgBox.addButton(self.tr("解锁"), -
QMessageBox.ActionRole) -
cancelButton=customMsgBox.addButton("cancel",QMessageBox.ActionRole) -
-
customMsgBox.setText(self.tr("这是一个自定义消息框!")) -
customMsgBox.exec_() -
-
button=customMsgBox.clickedButton() -
if button==lockButton: -
self.label.setText("Custom MessageBox/Lock") -
elif button==unlockButton: -
self.label.setText("Custom MessageBox/Unlock") -
elif button==cancelButton: -
self.label.setText("Custom MessageBox/Cancel") -
- app=QApplication(sys.argv)
- MessageBox=MessageBoxDlg()
- MessageBox.show()
- app.exec_()
本实例主要分析7种类型的消息框,包括Question消息框,Information消息框,Warning消息框,Critical消息框,About消息框,AboutQt消息框以及Custom自定义消息框。
Question消息框,Information消息框,Warning消息框和Critical消息框的用法大同小异,这些消息框一般都包含一条提示信息,一个图标以及若干个按钮,它们的作用都是给用户提供一些提醒或一些简单的询问。按图标的不同可区分为以下4个级另
Question:为正常的操作提供一个简单的询问。
Information:为正常的操作提供一个提示。
Warning:提醒用户发生了一个错误。
Critical:警告用户发生了一个严重错误。
下面分别对各种消息框的使用方法进行分析。
下图为Question消息框。
关于Question消息框,调用时直接使用QMessageBox.question()即可。
第一个参数为消息框的父窗口指针。
第二个参数为消息框的标题栏。
第三个参数为消息框的文字提示信息,前3个参数对于其他几种消息框基本是一样的。
后面两个参数都是对消息框按钮的设定,QMessageBox类提供了许多标准按钮,如QMessageBox.Ok,QMessageBox.Close,QMessageBox.Discard等,具体可查问Qt帮助。
第四个参数即填写希望在消息框中出现的按钮,可根据需要在标准按钮中选择,用“|”连写,默认为QMessageBox.Ok。
第五个参数为默认按钮,即消息框出现时,焦点默认处于哪个按钮上。
函数的返回值为按下的按钮,当用户按下Escape键时,相当于返回QMessageBox.Cancel。
如下图所示为Information消息框。
Information消息框使用频率最高也最简单,直接调用QMessageBox.information()即可。
第一个参数为消息框的父窗口指针。
第二个参数为消息框的标题栏。
第三个参数为消息框的文字提示信息。
后面的两个参数与Qustion消息框的用法一样,但在使用的过程中,经常会省略后两个参数,直接使用默认的QMessageBox.Ok按钮。
Information消息框和Question消息框可以通用,使用权Question消息框的地方都可以使用Information消息框替换。
如下图所示为Warning消息框。
Warning消息框的最常用法为当用户进行了一个非正常操作时,提醒用户并询问是否进行某项操作,如关闭文档,提醒并询问用户是否保存对文档的修改。实例中实现的即是此操作。
函数调用的方式与前面Question消息框的调用方式大致相同。
第一个参数为消息框的父窗口指针。
第二个参数为消息框的标题栏。
第三个参数为消息框的文字提示信息,
第四个参数为希望在消息框中出现的按钮,可根据需要在标准按钮中选择,用“|”连写,默认为QMessageBox.Ok。
第五个参数为默认按钮,即消息框出现时,焦点默认处于哪个按钮上。
如下图所示为Critical消息框。
Critical消息框是在系统出现严重错误时对用户进行提醒的。它的用法也相对简单,通常情况下和Information消息框一样,在调用时只填写前3个参数即可。
如下图所示为About消息框。
About消息框一般用于提供系统的版本等信息。只需提供信息而并不需要用户反馈信息,因此它的用法相对简单,直接调用QMessageBox.about(),并只用指定消息框父窗口,标题栏以及信息的内容即可。
在介绍完以上几种基本消息框的用法后,还有两种特殊的消息框类型,分别是“About Qt消息框”以及自定义消息框。
如下图所示为About Qt消息框。
“AboutQt消息框”是Qt预定好的一种消息框,用于提供Qt的相关信息,只需直接调用QMessageBox.aboutQt(),并提定父窗口和标题栏即可,其中显示的内容是Qt预定义好的。
最后,当以上所有的消息框都不能满足开发的需求时,Qt还允许Custom自定义消息框。包括消息框的图标,按钮,内容等都可根据需要进行设定。本实例中即实现了一个如下图所示的自定义消息框。
在slotCustom()函数中,第84行首先创建一个QMessageBox对象customMsgBox。第85行设置此消息框的标题栏为Custommessage box。
第86-90行定义消息框所需的按钮,因此QMessageBox类提供了一个addButton()函数来为消息框增加自定义按钮,addButton()函数的第一个参数为按钮显示的文字,第二个参数为按钮类型的描述,具体可查阅QMessageBox.ButtonRole,当然也可使用addButton()函数来加入一个标准按钮。如第90行在消息框中加入了一个QMessageBox.Cancel按钮。消息框将会按调用addButton()的先后次序在消息框中由左至右依次插入按钮。
第92行调用setText设置自定义消息框中显示的提示信息内容。
第93行调用exec()显示此自定义消息框。
后面几行代码完成的都是实例中一些显示的功能,此处不再讨论。
通过本实例的分析可见,Qt提供的消息框类型基本涵盖了开发应用中使用的各种情况,并且提供了自定义消息框的方式,满足各种特殊的需求,在实际应用中关键是分析实际的应用需求,根据不同的应用环境选择最合适的消息框,以使程序简洁而合理。