Fixed unicode issues with PySide loadUiType

Added unit test for loadUiType
This commit is contained in:
Luke Campagnola 2014-04-26 13:45:34 -04:00
parent 4e555e0bf3
commit 5a8d77d6f2
2 changed files with 29 additions and 3 deletions

View File

@ -11,6 +11,8 @@ This module exists to smooth out some of the differences between PySide and PyQt
import sys, re
from .python2_3 import asUnicode
## Automatically determine whether to use PyQt or PySide.
## This is done by first checking to see whether one of the libraries
## is already imported. If not, then attempt to import PyQt4, then PySide.
@ -56,18 +58,29 @@ if USE_PYSIDE:
# Credit:
# http://stackoverflow.com/questions/4442286/python-code-genration-with-pyside-uic/14195313#14195313
class StringIO(object):
"""Alternative to built-in StringIO needed to circumvent unicode/ascii issues"""
def __init__(self):
self.data = []
def write(self, data):
self.data.append(data)
def getvalue(self):
return ''.join(map(asUnicode, self.data)).encode('utf8')
def loadUiType(uiFile):
"""
Pyside "loadUiType" command like PyQt4 has one, 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.
"""
import pysideuic
import xml.etree.ElementTree as xml
from io import StringIO
#from io import StringIO
parsed = xml.parse(uiFile)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(uiFile, 'r') as f:
o = StringIO()
frame = {}

View File

@ -1,5 +1,7 @@
import pyqtgraph as pg
import gc
import gc, os
app = pg.mkQApp()
def test_isQObjectAlive():
o1 = pg.QtCore.QObject()
@ -8,3 +10,14 @@ def test_isQObjectAlive():
del o1
gc.collect()
assert not pg.Qt.isQObjectAlive(o2)
def test_loadUiType():
path = os.path.dirname(__file__)
formClass, baseClass = pg.Qt.loadUiType(os.path.join(path, 'uictest.ui'))
w = baseClass()
ui = formClass()
ui.setupUi(w)
w.show()
app.processEvents()