EDIT: Problem Solved
THE ANSWER IS NOT sys.exit()
The solution to the problem is using the ".accept" method in the ".connect" call. Since this is an instance of the QDialog class, you need to use ".accept".
self.alerts_window_cancel_button.clicked.connect(CreateEditAlertsWindow.accept)
This is the working solution to have a button close just the QDialog window and not the entire program.
Original Question:
I have searched probably 30 questions and none of the solutions are working for me although some of the original posters have a similar situation. I hate asking questions on here, but here we go...
I'm creating a GUI application with PyQt5 and QtDesigner and I have a main window that opens a separate dialog window. I would like to close the dialog window when the "cancel" button is pressed.
From reading other answers on here, I've tried:
self.button.clicked.connect(self.close())
self.button.clicked.connect(self.exit())
self.button.clicked.connect(self.reject)
As well as linking the button to a function:
self.btn.clicked.connect(self.closeWindow)
def closeWindow(self):
# self.exit()
# ui.close()
# app.close()
Here's my code. I tried to take out all of the irrelevant other stuff as this program is several hundred lines.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_CreateEditAlertsWindow(object):
def setupUi(self, CreateEditAlertsWindow):
self.alerts_window_cancel_button = QtWidgets.QPushButton(CreateEditAlertsWindow)
self.alerts_window_cancel_button.setObjectName ("alerts_window_cancel_button")
self.horizontalLayout_2.addWidget(self.alerts_window_cancel_button)
# a bunch of other stuff
def retranslateUi(self, CreateEditAlertsWindow):
_translate = QtCore.QCoreApplication.translate
self.alerts_window_cancel_button.setText (_translate("CreateEditAlertsWindow", "Cancel"))
self.alerts_window_cancel_button.clicked.connect(self.closeWindow)
# a bunch more stuff
def closeWindow(self):
self.exit()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
CreateEditAlertsWindow = QtWidgets.QDialog()
ui = Ui_CreateEditAlertsWindow()
ui.setupUi(CreateEditAlertsWindow)
CreateEditAlertsWindow.show()
sys.exit(app.exec_())
Edit: The issue I'm running into is I get an "AttributeError: "Ui_CreateEditAlertsWindow" object has no attribute "exit"