I have a Mainwindow that is opening a dialog on the press of a pushbutton. I want to override accept function of the dialog so I could implement some custom functionality that should be executed before dialog is accepted. Here is the example code of my mainwindow.
from PySide2 import QtCore, QtGui, QtWidgets
from add_new import Add_new_dialog
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 100)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushNew_Bed = QtWidgets.QPushButton(self.centralwidget)
self.pushNew_Bed.setObjectName("pushNew_Bed")
self.pushNew_Bed.resize(QtCore.QSize(500,100))
self.pushNew_Bed.clicked.connect(self.on_add_new_clicked)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushNew_Bed.setText(_translate("MainWindow", "Add New Bed"))
def on_add_new_clicked(self):
Dialog = QtWidgets.QDialog()
Dialog.ui = Add_new_dialog()
Dialog.ui.setupUi(Dialog)
#dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
retValue = Dialog.exec_()
if retValue == 1:
print("Accepted")#dialog.calendarWidget.selectedDate())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
And here is the dialog.
from PySide2 import QtCore, QtGui, QtWidgets
import sys
class Add_new_dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
Dialog.setMinimumSize(QtCore.QSize(400, 300))
Dialog.setMaximumSize(QtCore.QSize(400, 300))
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Dialog)
#self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.accepted.connect(self.custom_func)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
def custom_func(self):
Dialog.accept()
print("custom func")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Add_new_dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
If I launch the dialog standalone everything works as expected. However if I call for it from the mainwindow, I, predictably, get an error.
Dialog.accept()
NameError: name 'Dialog' is not defined
I've tried adding the following code to dialog to overwrite the accept function:
def accept(self):
self.custom_func()
Dialog.done(QtWidgets.QDialog.Accepted)
And binding the button to that function
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(Dialog.reject)
Again, predictably I get the same result. Reading the documentation didn't help much. Any help is greatly appreciated and I apologize for this noobish question, but I'm a complete beginner with gui. Thank you in advance.