Skip to content

Commit 9d1f3ed

Browse files
authored
PR: Replace custom implementation with loadUiType from PySide6 (#440)
2 parents b303b62 + 278d064 commit 9d1f3ed

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

qtpy/tests/test_uic.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import warnings
55

66
import pytest
7+
from packaging.version import parse
78

8-
from qtpy import PYSIDE6, PYSIDE2, QtWidgets
9+
from qtpy import PYSIDE6, PYSIDE2, QtWidgets, PYSIDE_VERSION
910
from qtpy.QtWidgets import QComboBox
11+
from qtpy.tests.utils import using_conda
1012

1113
if PYSIDE2:
1214
pytest.importorskip("pyside2uic", reason="pyside2uic not installed")
@@ -56,8 +58,9 @@ def test_load_ui(qtbot):
5658

5759

5860
@pytest.mark.skipif(
59-
PYSIDE2 or PYSIDE6,
60-
reason="PySide2uic not consistently installed across platforms/versions")
61+
PYSIDE6 and using_conda() and parse(PYSIDE_VERSION) < parse("6.5")
62+
and (sys.platform in ("darwin", "linux")),
63+
reason="pyside6-uic command not contained in all conda-forge packages.")
6164
def test_load_ui_type(qtbot):
6265
"""
6366
Make sure that the patched loadUiType function behaves as expected with a

qtpy/uic.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
if PYSIDE6:
8282
from PySide6.QtCore import QMetaObject
8383
from PySide6.QtUiTools import QUiLoader
84+
from PySide6.QtUiTools import loadUiType
8485
elif PYSIDE2:
8586
from PySide2.QtCore import QMetaObject
8687
from PySide2.QtUiTools import QUiLoader
@@ -245,41 +246,42 @@ def loadUi(uifile, baseinstance=None, workingDirectory=None):
245246
QMetaObject.connectSlotsByName(widget)
246247
return widget
247248

248-
def loadUiType(uifile, from_imports=False):
249-
"""Load a .ui file and return the generated form class and
250-
the Qt base class.
249+
if PYSIDE2:
250+
def loadUiType(uifile, from_imports=False):
251+
"""Load a .ui file and return the generated form class and
252+
the Qt base class.
251253
252-
The "loadUiType" command convert the ui file to py code
253-
in-memory first and then execute it in a special frame to
254-
retrieve the form_class.
254+
The "loadUiType" command convert the ui file to py code
255+
in-memory first and then execute it in a special frame to
256+
retrieve the form_class.
255257
256-
Credit: https://stackoverflow.com/a/14195313/15954282
257-
"""
258+
Credit: https://stackoverflow.com/a/14195313/15954282
259+
"""
258260

259-
import sys
260-
from io import StringIO
261-
from xml.etree.ElementTree import ElementTree
262-
263-
from . import QtWidgets
261+
import sys
262+
from io import StringIO
263+
from xml.etree.ElementTree import ElementTree
264264

265-
# Parse the UI file
266-
etree = ElementTree()
267-
ui = etree.parse(uifile)
265+
from . import QtWidgets
266+
267+
# Parse the UI file
268+
etree = ElementTree()
269+
ui = etree.parse(uifile)
268270

269-
widget_class = ui.find('widget').get('class')
270-
form_class = ui.find('class').text
271+
widget_class = ui.find('widget').get('class')
272+
form_class = ui.find('class').text
271273

272-
with open(uifile, encoding="utf-8") as fd:
273-
code_stream = StringIO()
274-
frame = {}
274+
with open(uifile, encoding="utf-8") as fd:
275+
code_stream = StringIO()
276+
frame = {}
275277

276-
compileUi(fd, code_stream, indent=0, from_imports=from_imports)
277-
pyc = compile(code_stream.getvalue(), '<string>', 'exec')
278-
exec(pyc, frame)
278+
compileUi(fd, code_stream, indent=0, from_imports=from_imports)
279+
pyc = compile(code_stream.getvalue(), '<string>', 'exec')
280+
exec(pyc, frame)
279281

280-
# Fetch the base_class and form class based on their type in the
281-
# xml from designer
282-
form_class = frame['Ui_%s' % form_class]
283-
base_class = getattr(QtWidgets, widget_class)
282+
# Fetch the base_class and form class based on their type in the
283+
# xml from designer
284+
form_class = frame['Ui_%s' % form_class]
285+
base_class = getattr(QtWidgets, widget_class)
284286

285-
return form_class, base_class
287+
return form_class, base_class

0 commit comments

Comments
 (0)