【问题标题】:How to subclass QMessageBox and run operation before closing如何在关闭之前继承 QMessageBox 并运行操作
【发布时间】:2018-02-08 19:54:38
【问题描述】:

我想在 QMessageBox 子类中启动一个进程,然后再通过 AcceptRole 返回。我不清楚为什么以下方法不起作用:

class Question(QtGui.QMessageBox):

    def __init__(self, text):
        QtGui.QMessageBox.__init__(self, QtGui.QMessageBox.Question, 
           "title", text, buttons=QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
        self.text = text

    def accept(self):

        # run opertation
        print self.text

        QtGui.QMessageBox.accept(self)

dial = Question("text")
dial.exec_()

由于 QMessageBox.Ok 的 buttonRole 是 AcceptRole,我希望 accept() 被调用。不是这样吗?

任何见解都将不胜感激。

【问题讨论】:

  • accept() 是一个槽,它不会被自动调用,你必须调用它,所以它永远不会被自动调用。

标签: python pyqt pyqt4 subclass qmessagebox


【解决方案1】:

你的想法是对的。您只需要重新实现虚拟done() 插槽,而不是虚拟accept() 插槽:

class Question(QtGui.QMessageBox):
    def __init__(self, text):
        QtGui.QMessageBox.__init__(
            self, QtGui.QMessageBox.Question, "title", text,
            buttons=QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
        self.text = text

    def done(self, result):
        print self.text
        QtGui.QMessageBox.done(self, result)

result 将是被点击按钮的StandardButton 值(即QMessageBox.OkQMessageBox.Cancel,在上面的示例中)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-23
    • 2019-02-19
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多