1

I'm using PySide6 now. I want Popup MessageBox when close window, So I Write like below

I have the following example code but closeEvent is never working.

1. main.py

from ui_mainwindow import Ui_MainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message',
                                     "Are you sure to quit?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

2. ui_mainwindow.py - autocreate from designer

how can I fix it?

HAMARU
  • 11
  • 1
  • show ui_mainwindow.py – eyllanesc Nov 25 '21 at 08:04
  • You're probably using the wrong Enums, since PyQt6 you need the full enumeration name. Run the program in a terminal or prompt and you'll probably see an AttributeError causing the crash. – musicamante Nov 25 '21 at 08:10
  • Does this answer your question? [Alternative to "QMessageBox.Yes" for PyQt6](https://stackoverflow.com/questions/65735260/alternative-to-qmessagebox-yes-for-pyqt6) – musicamante Nov 25 '21 at 08:13
  • I Create 'ui_mainwindow.py' use designer and 'pyside6-uic'. So, It's Start ## Form generated from reading UI file 'Main_Window.ui' I think 'ui_mainwindow.py' is may not be the cause. – HAMARU Nov 25 '21 at 11:24
  • I am having the same issue, I put a print statement in closeEvent() and it never prints either, meaning that this method is never even called when you press the X – Teddy_Tort Aug 09 '22 at 23:56
  • 1
    I just fixed it. For me, changing it to "close_event()" worked, but I also have snake_case enabled. – Teddy_Tort Aug 10 '22 at 00:04

0 Answers0