How do I create a critical error message using PySide?

前端 未结 2 484
慢半拍i
慢半拍i 2021-01-14 21:54

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:

相关标签:
2条回答
  • 2021-01-14 22:02

    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.

    0 讨论(0)
  • 2021-01-14 22:16

    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)
    
    0 讨论(0)
提交回复
热议问题