Add support for running pyside2-uic binary to dynamically compile ui files

This commit is contained in:
Luke Campagnola 2020-05-28 11:59:31 -07:00
parent 35128d404a
commit 6a76f40869

View File

@ -10,7 +10,7 @@ This module exists to smooth out some of the differences between PySide and PyQt
""" """
import os, sys, re, time import os, sys, re, time, subprocess
from .python2_3 import asUnicode from .python2_3 import asUnicode
@ -105,28 +105,45 @@ def _loadUiType(uiFile):
if QT_LIB == "PYSIDE": if QT_LIB == "PYSIDE":
import pysideuic import pysideuic
else: else:
import pyside2uic as pysideuic try:
import xml.etree.ElementTree as xml import pyside2uic as pysideuic
except ImportError:
# later vserions of pyside2 have dropped pysideuic; use the uic binary instead.
pysideuic = None
# get class names from ui file
import xml.etree.ElementTree as xml
parsed = xml.parse(uiFile) parsed = xml.parse(uiFile)
widget_class = parsed.find('widget').get('class') widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text form_class = parsed.find('class').text
with open(uiFile, 'r') as f: # convert ui file to python code
if pysideuic is None:
uipy = subprocess.check_output(['pyside2-uic', uiFile])
else:
o = _StringIO() o = _StringIO()
frame = {} with open(uiFile, 'r') as f:
pysideuic.compileUi(f, o, indent=0)
uipy = o.getvalue()
pysideuic.compileUi(f, o, indent=0) # exceute python code
pyc = compile(o.getvalue(), '<string>', 'exec') pyc = compile(uipy, '<string>', 'exec')
exec(pyc, frame) frame = {}
exec(pyc, frame)
#Fetch the base_class and form class based on their type in the xml from designer # fetch the base_class and form class based on their type in the xml from designer
form_class = frame['Ui_%s'%form_class] form_class = frame['Ui_%s'%form_class]
base_class = eval('QtGui.%s'%widget_class) base_class = eval('QtGui.%s'%widget_class)
return form_class, base_class return form_class, base_class
def _pyside2uic(uiFile):
glob = {}
mod = exec(uipy, globals=glob)
if QT_LIB == PYSIDE: if QT_LIB == PYSIDE:
from PySide import QtGui, QtCore from PySide import QtGui, QtCore