-
-
Save meramsey/0a0ff9943ba6ca44ca8a86b59efea7bc to your computer and use it in GitHub Desktop.
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 os | |
from cStringIO import StringIO | |
try: | |
import xml.etree.cElementTree as Xml | |
except ImportError: | |
import xml.etree.ElementTree as Xml | |
from PySide import QtGui # noqa | |
from PySide.QtCore import QFile | |
from PySide.QtUiTools import QUiLoader | |
from pysideuic import compileUi | |
def load_ui_type(uifile): | |
""" | |
PySide lacks the "loadUiType" command, so we have to convert the ui file to py code in-memory first | |
and then execute it in a special frame to retrieve the form_class. | |
""" | |
parsed_xml = Xml.parse(uifile) | |
form_class = parsed_xml.find('class').text | |
widget_class = parsed_xml.find('widget').get('class') | |
with open(uifile, 'r') as _input: | |
_output = StringIO() | |
compileUi(_input, _output, indent=0) | |
source_code = _output.getvalue() | |
syntax_tree = compile(source_code, filename='<string>', mode='exec') | |
scope = {} | |
exec(syntax_tree, scope) | |
# Fetch the base_class and form class based on their type in the xml from designer | |
form_class = scope['Ui_{}'.format(form_class)] | |
base_class = eval('QtGui.{}'.format(widget_class)) | |
return form_class, base_class | |
ui_path = os.path.join('some', 'path', 'to', 'your.ui') | |
form_class, base_class = load_ui_type(ui_path) | |
class YOUR_UI(base_class, form_class): | |
def __init__(self, args): | |
super(YOUR_UI, self).__init__() | |
# build UI | |
self.setupUi(self) | |
# etc ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment