Skip to content

Commit 99db418

Browse files
committed
feat(qt-examples): add pyqt6__ask_before_close.py
1 parent 657d0a7 commit 99db418

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = "ipetrash"
5+
6+
7+
import sys
8+
9+
from PyQt6.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout, QMessageBox
10+
from PyQt6.QtGui import QCloseEvent
11+
12+
13+
class MainWindow(QWidget):
14+
def __init__(self) -> None:
15+
super().__init__()
16+
17+
button = QPushButton("Close")
18+
button.clicked.connect(self.close)
19+
20+
layout = QVBoxLayout()
21+
layout.addWidget(button)
22+
23+
self.setLayout(layout)
24+
25+
def closeEvent(self, event: QCloseEvent | None) -> None:
26+
if not event:
27+
return
28+
29+
reply = QMessageBox.question(
30+
self,
31+
"Quit",
32+
"Are you sure you want to quit?",
33+
QMessageBox.StandardButton.Yes,
34+
QMessageBox.StandardButton.No,
35+
)
36+
37+
if reply == QMessageBox.StandardButton.Yes:
38+
event.accept()
39+
else:
40+
event.ignore()
41+
42+
43+
if __name__ == "__main__":
44+
app = QApplication(sys.argv)
45+
46+
mw = MainWindow()
47+
mw.show()
48+
49+
sys.exit(app.exec())

0 commit comments

Comments
 (0)