0

I have a window and I want to have a close event for that window. How to detect the closeing of the dialog (menu.py) in the main class.

main.py

class QMain(QDialog, QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()        
        self.winMenu = QDialog()
        self.uiMenu = menu.Ui_Dialog()
        self.uiMenu.setupUi(self.winMenu)
        self.winMenu.show()

app = QApplication([])    
w = QMain()
sys.exit(app.exec_())

menu.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(333, 290)
        Dialog.setWindowFlag(QtCore.Qt.WindowMinimizeButtonHint, True)  
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(30, 60, 261, 51))
        self.pushButton.setObjectName("pushButton") 
        self.pushButton.setIcon(QtGui.QIcon('Images/documents.png'))
        self.pushButton.setIconSize(QtCore.QSize(35,35))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "MC"))
        self.pushButton.setText(_translate("Dialog", "Փաստաթուղթ"))     
mchovo
  • 11
  • 5
  • 1) are you sure that you want to create a main window and not use it? 2) why do you use the multiple inheritance method *and* the setupUi method? 3) is that dialog going to be modal? – musicamante Jan 12 '21 at 12:12
  • Thanks for your comment but I have already found the solution. 1. uic.loadUi('menu.ui', self) 2. def closeEvent(self, event): reply = QMessageBox.question(self, 'Quit', 'Are you sure you want to quit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore() – mchovo Jan 12 '21 at 13:27
  • Note that (1) is potentially wrong: the ui file is for a QDialog, while you're using loadUi on a QMainWindow. Then, if you correctly use a QDialog instead, and you also correctly use the QDialog `exec_()` to launch it, the `closeEvent()` is *not* processed if the `Esc` key is pressed. – musicamante Jan 12 '21 at 13:59
  • Yes you are right it doesn't work Esc key – mchovo Jan 14 '21 at 09:17

0 Answers0