Created
September 24, 2021 23:50
-
-
Save Majramos/4a35b995be5879cef6726c0d6093eb57 to your computer and use it in GitHub Desktop.
Using a .ui file from Qt Designer with Pyside6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
from PySide6.QtWidgets import QApplication, QMessageBox | |
from PySide6.QtUiTools import loadUiType | |
from PySide6.QtGui import QCloseEvent | |
CWD = os.path.dirname(__file__) | |
# Use this instead of QUiLoader. PySide6 loader does not allow you to apply | |
# a UI layout to an existing widget. This prevents you adding custom code for | |
# the initialization of the widget in a class __init__ block | |
uiclass, baseclass = loadUiType(os.path.join(CWD, 'mainwindow.ui')) | |
# baseclass will be the QMainWindow | |
class MainWindow(uiclass, baseclass): | |
def __init__(self): | |
super().__init__() | |
self.setupUi(self) | |
def closeEvent(self, event: QCloseEvent): | |
reply = QMessageBox.question(self, 'Leaving...', 'Are you sure you want 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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment