I am writing an app which has the login window as a separate window/class. So far, everything works as expected, but I am having trouble figuring out how to get the main window to detect when the login window closes. Does anyone have an idea as to how to make that work?
I thought that the closeEvent() method would help, but apparanty, Qwidget doesn’t have an option for it.
@pythonnoob
Without some code to see what you are trying it’s hard to say for sure what needs to change. But, I can say that if I wanted to have a login window for my app I would do something like the following pseudo code:
class LoginDialog(QDialog):
"""Class that defines my login window"""
def __init__(self):
super().__init__()
# Define the layout and functionality of the login dialog here
class MainWindow(QMainWindow):
"""Class that defines my main application window"""
def __init__(self):
super().__init__()
# Define the layout and functionality of the main window here
# Call the login dialog
login_dialog = LoginDialog()
if login_dialog.exec():
# Enters here if dialog returns with a QDialog.accept
else:
# Enters here is dialog returns with a QDialog.reject
I hope this helps. Building a QDialog is very similar to a a QMainWindow. The one thing I’d be sure to include in the dialog is the use of a QDialogButtonBox for the Ok and Cancel buttons (or whatever you want to call them). Then you can assign accept and reject directly to the button box.
btns = QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
self.button_box = QDialogButtonBox(btns)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
Here’s a link to the QDialogButtonBox documentation: https://doc.qt.io/qt-6/qdialogbuttonbox.html
Good Luck!