Last active
April 12, 2023 17:55
-
-
Save tcrowson/11e70cfc9c3bcb09e8d8 to your computer and use it in GitHub Desktop.
Dynamically Load UI file from QtDesigner
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
from PySide.QtGui import * | |
from PySide.QtCore import * | |
from PySide.QtUiTools import QUiLoader | |
uiFile = "path\to\my\UI\file.ui" | |
class UiLoader(QUiLoader): | |
''' | |
Convenience class for dynamically loading QtDesigner '.ui' files, | |
circumventing the need to convert them into Python modules via pyside-uic. | |
''' | |
def __init__(self, baseinstance): | |
QUiLoader.__init__(self, baseinstance) | |
self.baseinstance = baseinstance | |
def createWidget(self, class_name, parent=None, name=''): | |
if parent is None and self.baseinstance: | |
return self.baseinstance | |
else: | |
widget = QUiLoader.createWidget(self, class_name, parent, name) | |
if self.baseinstance: | |
setattr(self.baseinstance, name, widget) | |
return widget | |
class MyWidget(QtGui.QWidget): | |
''' ''' | |
def __init__(self, parent=None): | |
''' ''' | |
QWidget.__init__(self, parent) | |
self.loadUi(uiFile, self) | |
#self.someWidget.someSignal.connect(self.close) | |
def loadUi(self, uifile, baseinstance=None): | |
''' Dynamically load the UI file ''' | |
loader = UiLoader(baseinstance) | |
widget = loader.load(uifile) | |
QMetaObject.connectSlotsByName(widget) | |
return widget | |
win = MyWidget() | |
win.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment