User Tools

Site Tools


python:qmessagebox

QMessageBox

The QMessageBox class provides a modal dialog with a short message, an icon, and buttons laid out depending on the current style.
Message boxes are used to provide informative messages and to ask simple questions.

QMessageBox built-in Qt dialogs

Question Information Warning Critical Simple About box

Python code to exemplify the dialogs:

Application

# QMessageBox examples
# python 2.6 Qt 4.2.1
 
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
 
 
class Example(QDialog):
 
    def __init__(self, parent=None):
        super(Example, self).__init__(parent)
 
        self.resize(260,160)
 
 
        layout = QVBoxLayout()      
 
        for text, slot in (("About", self.about),
                           ("Information", self.info),
                           ("Warning", self.warning),
                           ("Critical", self.critical),
                           ("Question", self.question),
                           ("Custom standard buttons", self.standardbuttons),
                           ("Custom buttons", self.custombuttons)):
            button = QPushButton(text)
            layout.addWidget(button)
            self.connect(button, SIGNAL("clicked()"), slot)
 
        self.setLayout(layout)
        self.setWindowTitle('QMessageBox Example')
 
    def about(self):
        # QMessageBox.about (QWidget parent, QString caption, QString text)
        QMessageBox.about(self, 'About Message',
            '''About Box.<br />
                This accepts HTML formatting <b> bold</b>''')
 
    def info(self):
        # QMessageBox.information (QWidget parent, QString caption, QString text,
        # int button0, int button1 = 0, int button2 = 0)
        QMessageBox.information(self, 'Info Message', ''' Info Message Box''',
            QMessageBox.Ok)
 
    def question(self):
        # int QMessageBox.question (QWidget parent, QString caption, QString text, 
        # int button0, int button1 = 0, int button2 = 0),
        # QMessageBox.Ok 	0x00000400 	An "OK" button defined with the AcceptRole.
        # QMessageBox.Open 	0x00002000 	A "Open" button defined with the AcceptRole.
        # QMessageBox.Save 	0x00000800 	A "Save" button defined with the AcceptRole.
        # QMessageBox.Cancel 	0x00400000 	A "Cancel" button defined with the RejectRole.
        # QMessageBox.Close 	0x00200000 	A "Close" button defined with the RejectRole.
        # QMessageBox.Discard 	0x00800000 	A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.
        # QMessageBox.Apply 	0x02000000 	An "Apply" button defined with the ApplyRole.
        # QMessageBox.Reset 	0x04000000 	A "Reset" button defined with the ResetRole.
        # QMessageBox.RestoreDefaults 	0x08000000 	A "Restore Defaults" button defined with the ResetRole.
        # QMessageBox.Help 	0x01000000 	A "Help" button defined with the HelpRole.
        # QMessageBox.SaveAll 	0x00001000 	A "Save All" button defined with the AcceptRole.
        # QMessageBox.Yes 	0x00004000 	A "Yes" button defined with the YesRole.
        # QMessageBox.YesToAll 	0x00008000 	A "Yes to All" button defined with the YesRole.
        # QMessageBox.No 	0x00010000 	A "No" button defined with the NoRole.
        # QMessageBox.NoToAll 	0x00020000 	A "No to All" button defined with the NoRole.
        # QMessageBox.Abort 	0x00040000 	An "Abort" button defined with the RejectRole.
        # QMessageBox.Retry 	0x00080000 	A "Retry" button defined with the AcceptRole.
        # QMessageBox.Ignore 	0x00100000 	An "Ignore" button defined with the AcceptRole.
        # QMessageBox.NoButton 	0x00000000 	An invalid button.
 
        QMessageBox.question(self, 'Question Message', 
            '''
            This is a question.
            To beer or not to beer?
            ''',
            QMessageBox.Ok, QMessageBox.Cancel, QMessageBox.Abort)
 
    def warning(self):
        ret = QMessageBox.warning(self, "Warning",
                '''The document has been modified.\n
                   Do you want to save your changes?''',
                QMessageBox.Save, QMessageBox.Discard, QMessageBox.Cancel);
        print "Answer: %s" % ret
 
    def critical(self):
        # StandardButton QMessageBox.critical (QWidget parent, QString title, 
        # QString text, StandardButtons buttons = QMessageBox.Ok, StandardButton 
        # defaultButton = QMessageBox.NoButton)
        ret = QMessageBox.critical(self, "Critical", 
              '''Critical! No booze left!''', QMessageBox.Ok, QMessageBox.Cancel)
 
        if ret == QMessageBox.Ok:
            QMessageBox.information(self, 'Info Message', '''You pressed OK''',
            QMessageBox.Ok)
        else:
            QMessageBox.information(self, 'Info Message', '''You pressed Cancel''',
            QMessageBox.Ok)
 
    def standardbuttons(self):
        msgBox = QMessageBox()
        msgBox.setText('Are you sure?')
        msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Discard | QMessageBox.Cancel)
        # msgBox.setDefaultButton(QMessageBox.Ok) # This function was introduced in Qt 4.3.
        ret = msgBox.exec_();
 
    def custombuttons(self):
        msgBox = QMessageBox()
        msgBox.setText('How do you want the pizza?')
        btnQS   = QPushButton('Quattro Staggioni')
        msgBox.addButton(btnQS, QMessageBox.YesRole)
        btnNo   = QPushButton('No thanks, just simple')
        msgBox.addButton(btnNo, QMessageBox.NoRole)
        ret = msgBox.exec_();
 
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Example()
    form.exec_()

python/qmessagebox.txt · Last modified: 2013/03/16 17:40 (external edit)