Skip to content

Instantly share code, notes, and snippets.

@tcrowson
Last active April 12, 2023 17:55
Show Gist options
  • Save tcrowson/11e70cfc9c3bcb09e8d8 to your computer and use it in GitHub Desktop.
Save tcrowson/11e70cfc9c3bcb09e8d8 to your computer and use it in GitHub Desktop.
Dynamically Load UI file from QtDesigner
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