1

I have a window/widget that shows after I press a button, is there a way to make the main window wait until the widget closes out? I am using .show() right now and I have tried using .exec_() already but it gives me this error:

AttributeError: 'MainWindow' object has no attribute 'exec_'

Any help?

László Papp
  • 51,870
  • 39
  • 111
  • 135
user2638731
  • 595
  • 2
  • 15
  • 28
  • I think you want a modal dialog http://stackoverflow.com/questions/7917339/qt-modal-dialog-and-main-process – tacaswell Oct 19 '13 at 04:35

1 Answers1

4

Use a local event-loop to wait until the window/widget closes:

widget = QWidget()
widget.setAttribute(Qt.WA_DeleteOnClose)
widget.show()
loop = QEventLoop()
widget.destroyed.connect(loop.quit)
loop.exec() # wait ...
print('finished')

To also block interaction with other windows, set the window modality:

widget.setWindowModality(Qt.ApplicationModal)

or for top-level windows with a parent:

window.setWindowModality(Qt.WindowModal)

Of course, if you can change the window/widget to a QDialog, then none of the above is necessary, since the same functionality is provided by exec:

widget = QDialog()
widget.exec() # wait ...
ekhumoro
  • 115,249
  • 20
  • 229
  • 336