1

I would like to launch a process within a QMessageBox subclass before it returns via the AcceptRole. It's not clear to me why the following does not work:

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_()

Since the buttonRole for QMessageBox.Ok is AcceptRole, I would expect accept() to be called. Is this not the case?

Any insight would be appreciated.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
tom voll
  • 55
  • 5
  • accept() is a slot, it is not called automatically, you have to call it so it will never be called automatically. – eyllanesc Feb 08 '18 at 20:25

1 Answers1

2

You have the right idea. You just need to reimplement the virtual done() slot, rather than the virtual accept() slot:

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)

The result will be StandardButton value of the button that was clicked (i.e. QMessageBox.Ok or QMessageBox.Cancel, in the above example).

ekhumoro
  • 115,249
  • 20
  • 229
  • 336