簡體   English   中英

在按QMessageBox的“確定”按鈕時不要關閉對話框

[英]Don’t close Dialog on pressing OK button of QMessageBox

我已經這樣調用QMessageBox()

class Main(QDialog):
    def __init__(self):
        self.view = QUiLoader().load("app.ui", self)
        self.view.show()
        self.functionA()
    ....
    functionA():
        try:
            ....
        except Exception:
            QMessageBox.critical(self, "Error", "System Failure")

def main():
    app = QApplication(sys.argv)
    a = Main()
    sys.exit(app.exec_())

if __name__ == "__main__"
    main()

當我單擊“消息”框的“確定”按鈕時,它也會關閉我的對話框 如何避免這種情況?

您的代碼示例(稍作更改即可運行)對我而言有效:

from PySide.QtGui import *

class Main(QDialog):
    def __init__(self):
        super().__init__()
        self.show()
        self.functionA()

    def functionA(self):
        try:
            raise Exception()
        except Exception:
            QMessageBox.critical(self, "Error", "System Failure")

app = QApplication([])
a = Main()
app.exec_()

您可以在消息框中按“確定”,對話框不會關閉。 您可能還在做其他事情導致對話框關閉。

像這樣使用QMessageBox

QMessageBox.critical(self.view, "Error", "System Failure")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM