title: pyqt how to create a wait popup for running a long function/calulation
body: <p>I am trying to create a dialog window that pops up, runs a function and then automatically closes when the function is done.  In the code below, the function runs before popping up the dialog and I cannot automatically close otherwise the dialog window will pop up and not respond to clicking the "x" button.</p>
  
  <p>How can I create a pop-up, run a function after the pop-up is visible, and then close the pop-up when the function is done running.</p>
  
  <pre><code># from PyQt4 import QtGui
  # QtWidgets = QtGui
  from PyQt5 import QtWidgets, QtCore
  import sys, time
  
  app = QtWidgets.QApplication(sys.argv)
  
  
  def print_every_3_seconds():
      print(0)
      for i in range(1, 4):
          time.sleep(1)
          print(i)
  
  
  class RunFunctionDialog(QtWidgets.QDialog):
  
      def __init__(self, function, parent=None):
          super(RunFunctionDialog, self).__init__(parent)
          self.layout = QtWidgets.QVBoxLayout(self)
          self.textBrowser = QtWidgets.QTextBrowser()
          self.textBrowser.setText("Wait 3 seconds")
          self.layout.addWidget(self.textBrowser)
          self.function = function
  
      def showEvent(self, QShowEvent):
          self.function()
          # self.close()  # dialog freezes in an unresponsive state
  
  
  def show_dialog():
      dialog = RunFunctionDialog(print_every_3_seconds)
      dialog.exec_()
  
  widget = QtWidgets.QWidget(None)
  button = QtWidgets.QPushButton("Show Dialog", widget)
  button.clicked.connect(show_dialog)
  
  widget.show()
  
  app.exec_()
  </code></pre>