0

Just getting into playing with this. Not essential for me at the moment, but still curious. Is there some way to create a timed qmessagebox?

Basically, I have a loop that is happening and takes a while between iterations. I want to display a message box to the user after each iteration completes so they have a chance to cancel out of future iterations, if they want. I also do not want the user to have to be there for the process to continue, so if say 6 seconds pass it will just close the message box and continue on.

Thinking this might be a bit tricky since the message boxes are modal and interrupt the flow of the script.

Anybody out there have ideas for how to maybe achieve this?

Mathieson
  • 1,194
  • 2
  • 13
  • 28

1 Answers1

1

As suggested in this answer, you could subclass QMessageBox to start a timer when showed. That calls close on itself when finished.

class timedMessageBox(QtGui.QMessageBox):
    def __init__(self, timeout, message):
        super(timedMessageBox, self).__init__()
        self.timeout = timeout
        timeoutMessage = "Closing in {} seconds".format(timeout)
        self.setText('\n'.join((message, timeoutMessage)))

    def showEvent(self, event):
        QtCore.QTimer().singleShot(self.timeout*1000, self.close)
        super(timedMessageBox, self).showEvent(event)
Community
  • 1
  • 1
M4rtini
  • 13,186
  • 4
  • 35
  • 42