diff --git a/qtpy/tests/test_uic.py b/qtpy/tests/test_uic.py index 578093e3..8cde0311 100644 --- a/qtpy/tests/test_uic.py +++ b/qtpy/tests/test_uic.py @@ -4,9 +4,11 @@ import warnings import pytest +from packaging.version import parse -from qtpy import PYSIDE6, PYSIDE2, QtWidgets +from qtpy import PYSIDE6, PYSIDE2, QtWidgets, PYSIDE_VERSION from qtpy.QtWidgets import QComboBox +from qtpy.tests.utils import using_conda if PYSIDE2: pytest.importorskip("pyside2uic", reason="pyside2uic not installed") @@ -56,8 +58,9 @@ def test_load_ui(qtbot): @pytest.mark.skipif( - PYSIDE2 or PYSIDE6, - reason="PySide2uic not consistently installed across platforms/versions") + PYSIDE6 and using_conda() and parse(PYSIDE_VERSION) < parse("6.5") + and (sys.platform in ("darwin", "linux")), + reason="pyside6-uic command not contained in all conda-forge packages.") def test_load_ui_type(qtbot): """ Make sure that the patched loadUiType function behaves as expected with a diff --git a/qtpy/uic.py b/qtpy/uic.py index 6f53e4f5..a51b193f 100644 --- a/qtpy/uic.py +++ b/qtpy/uic.py @@ -81,6 +81,7 @@ if PYSIDE6: from PySide6.QtCore import QMetaObject from PySide6.QtUiTools import QUiLoader + from PySide6.QtUiTools import loadUiType elif PYSIDE2: from PySide2.QtCore import QMetaObject from PySide2.QtUiTools import QUiLoader @@ -245,41 +246,42 @@ def loadUi(uifile, baseinstance=None, workingDirectory=None): QMetaObject.connectSlotsByName(widget) return widget - def loadUiType(uifile, from_imports=False): - """Load a .ui file and return the generated form class and - the Qt base class. + if PYSIDE2: + def loadUiType(uifile, from_imports=False): + """Load a .ui file and return the generated form class and + the Qt base class. - The "loadUiType" command convert the ui file to py code - in-memory first and then execute it in a special frame to - retrieve the form_class. + The "loadUiType" command convert the ui file to py code + in-memory first and then execute it in a special frame to + retrieve the form_class. - Credit: https://stackoverflow.com/a/14195313/15954282 - """ + Credit: https://stackoverflow.com/a/14195313/15954282 + """ - import sys - from io import StringIO - from xml.etree.ElementTree import ElementTree - - from . import QtWidgets + import sys + from io import StringIO + from xml.etree.ElementTree import ElementTree - # Parse the UI file - etree = ElementTree() - ui = etree.parse(uifile) + from . import QtWidgets + + # Parse the UI file + etree = ElementTree() + ui = etree.parse(uifile) - widget_class = ui.find('widget').get('class') - form_class = ui.find('class').text + widget_class = ui.find('widget').get('class') + form_class = ui.find('class').text - with open(uifile, encoding="utf-8") as fd: - code_stream = StringIO() - frame = {} + with open(uifile, encoding="utf-8") as fd: + code_stream = StringIO() + frame = {} - compileUi(fd, code_stream, indent=0, from_imports=from_imports) - pyc = compile(code_stream.getvalue(), '', 'exec') - exec(pyc, frame) + compileUi(fd, code_stream, indent=0, from_imports=from_imports) + pyc = compile(code_stream.getvalue(), '', 'exec') + 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 = getattr(QtWidgets, 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 = getattr(QtWidgets, widget_class) - return form_class, base_class + return form_class, base_class