Last active
February 3, 2021 16:12
-
-
Save foundry-markf/c2552b32f67720cf7f925d71a33e2186 to your computer and use it in GitHub Desktop.
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>EmptyDialog</class> | |
<widget class="EmptyDialog" name="EmptyDialog"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>400</width> | |
<height>300</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>EmptyDialog</string> | |
</property> | |
</widget> | |
<resources/> | |
<connections/> | |
</ui> |
This file contains 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 logging | |
import os | |
from PySide2.QtWidgets import QApplication, QDialog, QMainWindow, QPushButton, QVBoxLayout, QWidget | |
from PySide2.QtUiTools import QUiLoader | |
import PySide2.QtCore | |
def SomethingWithExternalResources_qt_on_destroyed(obj): | |
logging.critical("SomethingWithExternalResources Qt object destroyed") | |
class SomethingWithExternalResources(PySide2.QtCore.QObject): | |
def __init__(self, parent): | |
super().__init__(parent) | |
logging.critical("External resource __init__ called") | |
self.destroyed.connect(SomethingWithExternalResources_qt_on_destroyed) | |
def __del__(self): | |
logging.critical("External resource __del__ called") | |
def EmptyDialog_qt_on_destroyed(obj): | |
logging.critical("EmptyDialog Qt object destroyed") | |
class EmptyDialog(QDialog): | |
def __init__(self, parent = None): | |
super().__init__(parent) | |
self.destroyed.connect(EmptyDialog_qt_on_destroyed) | |
self.setAttribute(PySide2.QtCore.Qt.WA_DeleteOnClose, True) | |
self._resources = SomethingWithExternalResources(self) | |
logging.critical("EmptyDialog __init__ called") | |
def __del__(self): | |
logging.critical("EmptyDialog __del__ called") | |
class MainWindow(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
central_widget = QWidget() | |
self.setCentralWidget(central_widget) | |
layout = QVBoxLayout(central_widget) | |
button_for_code = QPushButton("From code") | |
button_for_code.clicked.connect(self._open_code_dialog) | |
layout.addWidget(button_for_code) | |
button_for_ui_file = QPushButton("From UI file") | |
button_for_ui_file.clicked.connect(self._open_ui_dialog) | |
layout.addWidget(button_for_ui_file) | |
def _open_ui_dialog(self): | |
logging.critical("Opening dialog from UI file") | |
loader = QUiLoader(self) | |
loader.registerCustomWidget(EmptyDialog) | |
base_dir = os.path.dirname(os.path.realpath(__file__)) | |
dialog = loader.load(os.path.join(base_dir, 'empty_dialog.ui'), self) | |
dialog.exec_() | |
logging.critical("Completed dialog from UI file") | |
def _open_code_dialog(self): | |
logging.critical("Opening dialog from code") | |
dialog = EmptyDialog(self) | |
dialog.setWindowTitle("Dialog from code") | |
dialog.exec_() | |
logging.critical("Completed dialog from code") | |
def callback(*args, **kwargs): | |
logging.critical('MessageHandler: %s %s', args, kwargs) | |
if __name__ == '__main__': | |
logging.critical("PySide2 version: %s", PySide2.__version__) | |
# only to suppress a warning about QWebEngine | |
PySide2.QtCore.QCoreApplication.setAttribute(PySide2.QtCore.Qt.AA_ShareOpenGLContexts) | |
PySide2.QtCore.qInstallMessageHandler(callback) | |
app = QApplication([]) | |
main_window = MainWindow() | |
main_window.show() | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment