From 6a76f40869a02d9561d008cf5fa1db0abc67b5b9 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Thu, 28 May 2020 11:59:31 -0700 Subject: [PATCH] Add support for running pyside2-uic binary to dynamically compile ui files --- pyqtgraph/Qt.py | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/pyqtgraph/Qt.py b/pyqtgraph/Qt.py index 702bc2bd..693ac46e 100644 --- a/pyqtgraph/Qt.py +++ b/pyqtgraph/Qt.py @@ -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 @@ -105,28 +105,45 @@ def _loadUiType(uiFile): if QT_LIB == "PYSIDE": import pysideuic else: - import pyside2uic as pysideuic - import xml.etree.ElementTree as xml + try: + 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) widget_class = parsed.find('widget').get('class') 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() - frame = {} + with open(uiFile, 'r') as f: + pysideuic.compileUi(f, o, indent=0) + uipy = o.getvalue() - pysideuic.compileUi(f, o, indent=0) - pyc = compile(o.getvalue(), '', 'exec') - exec(pyc, frame) + # exceute python code + pyc = compile(uipy, '', 'exec') + frame = {} + exec(pyc, frame) - #Fetch the base_class and form class based on their type in the xml from designer - form_class = frame['Ui_%s'%form_class] - base_class = eval('QtGui.%s'%widget_class) + # fetch the base_class and form class based on their type in the xml from designer + form_class = frame['Ui_%s'%form_class] + base_class = eval('QtGui.%s'%widget_class) return form_class, base_class +def _pyside2uic(uiFile): + glob = {} + mod = exec(uipy, globals=glob) + + + if QT_LIB == PYSIDE: from PySide import QtGui, QtCore