Issue
I have a PyQt5
window that will open a dialog window that I have created that just asked the user to do something and click a button that closes the window. The two windows are made with class RenameDialog(QtWidgets.QMainWindow):
and class Prog(QtWidgets.QMainWindow):
. and in Prog
, I have self.renameDialog = RenameDialog(self)
. In a piece of the code I have
self.renameDialog.show()
#Other code to run after renameDialog window is closed
But this does not work since I cannot figure out how to wait until renameDialog
is closed. I have tried putting self.renameDialog.setWindowModality(QtCore.Qt.WindowModality)
before self.renameDialog.show()
and I was trying to figure out how to use .exec_()
but do not know where I can use this method in this context. Is there a way to wait until this QtWidgets.QMainWindow
is hidden or destroyed before continuing the code?
Solution
You have two possibities here.
1.You can set the dialog as modal. In this case any other interaction with the GUI is blocked until the user closes the dialog. Only then the code is continued. You need to inherit from QDialog for this to work:
class RenameDialog(QDialog):
# ...
pass
class YourMainWindow(QMainWindow):
def show_dlg(self):
dlg = RenameDialog(self)
res = dlg.exec()
if res == QDialog.Accepted:
print('Accepted')
else:
print('Rejected')
- Connect to the finished() signal of your dialog. This way the dialog does not have to be modal and you can still interact with the rest of your GUI. When the user closes the dialog the
finished
signal gets triggered and the connected callback function gets called.
Answered By - MrLeeh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.