How do I create a critical error message using PyS

2019-02-19 11:37发布

问题:

I seem to be hitting a brick wall. No matter what I do, creating a critical error Message Box just doesn't seem to be working. Here's what I've tried thus far:

flags = QtGui.QMessageBox.StandardButton.Abort
flags |= QtGui.QMessageBox.StandardButton.Ignore

result = QtGui.QMessageBox.critical(
    self,
    'CRITICAL ERROR',
    'Error Message',
    flags
    )

As taken from this tutorial (old I know, but it's been helpful thus far). Doing this however, brings up the following error:

'PySide.QtGui.QMessageBox.critical' called with wrong argument types:

PySide.QtGui.QMessageBox.critical(CreateMessage, str, StandardButtons)

Supported signatures:

PySide.QtGui.QMessageBox.critical(PySide.QtGui.QWidget, unicode, unicode, PySide.QtGui.QMessageBox.StandardButtons = QMessageBox.Ok, PySide.QtGui.QMessageBox.StandardButton = NoButton)

PySide.QtGui.QMessageBox.critical(PySide.QtGui.QWidget, unicode, unicode, PySide.QtGui.QMessageBox.StandardButton, PySide.QtGui.QMessageBox.StandardButton)

I've also tried the following:

result = QtGui.QMessageBox.critical(
    self,
    'CRITICAL ERROR',
    'Error Message',
    QtGui.QMessageBox.StandardButton.Abort
    )

# Or this....

result = QtGui.QMessageBox.critical(
    self,
    'CRITICAL ERROR', 
    'Error Message',
    QtGui.QMessageBox.Abort
    )

None of these seem to work properly. How do I create a critical error message box?

回答1:

Here's an example from Qt.Gitorious.

from PySide import QtGui, QtCore
import sys

class Dialog(QtGui.QDialog):
    MESSAGE = QtCore.QT_TR_NOOP("<p>Message boxes have a caption, a text, and up to "
                                "three buttons, each with standard or custom texts.</p>"
                                "<p>Click a button or press Esc.</p>")

    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.criticalLabel = QtGui.QLabel()
        self.criticalLabel.setFrameStyle(QtGui.QFrame.Sunken | QtGui.QFrame.Panel)
        self.criticalButton = QtGui.QPushButton(self.tr("QMessageBox.critica&l()"))

        layout = QtGui.QGridLayout()
        layout.addWidget(self.criticalButton, 10, 0)
        layout.addWidget(self.criticalLabel, 10, 1)
        self.setLayout(layout)

        self.connect(self.criticalButton, QtCore.SIGNAL("clicked()"), self.criticalMessage)


    def criticalMessage(self):    
        reply = QtGui.QMessageBox.critical(self, self.tr("QMessageBox.showCritical()"),
                                               Dialog.MESSAGE, QtGui.QMessageBox.Abort|
                                               QtGui.QMessageBox.StandardButton.Retry|
                                               QtGui.QMessageBox.StandardButton.Ignore)
        if reply == QtGui.QMessageBox.Abort:
            self.criticalLabel.setText(self.tr("Abort"))
        elif reply == QtGui.QMessageBox.Retry:
            self.criticalLabel.setText(self.tr("Retry"))
        else:
            self.criticalLabel.setText(self.tr("Ignore"))

if __name__ == '__main__':  
    app = QtGui.QApplication(sys.argv)
    dialog = Dialog()
    sys.exit(dialog.exec_())        

To answer your question you can check the documentation:

static PySide.QtGui.QMessageBox.critical(parent, title, text[, buttons=QMessageBox.Ok[, defaultButton=NoButton]])

In the example, parent = self, title = self.tr("QMessageBox.showCritical()"), text = Dialog.MESSAGE, buttons = QtGui.QMessageBox.Abort | QtGui.QMessageBox.StandardButton.Retry | QtGui.QMessageBox.StandardButton.Ignore

The tr is just some Qt function to set up translations, basically its a string. I can't really tell you what you did wrong, looking at the error message, it seems to have parsed things wrong. Possibly because of the way you assigned values to flags.

The example also shows how to deal with the result of the critical dialog, which seems useful.



回答2:

Simple Example Below

import sys
from PySide import QtGui
app = QtGui.QApplication(sys.argv)
a=QtGui.QMessageBox.critical(None,'Error!',"Error Message!", QtGui.QMessageBox.Abort)