From f7d3dae7faa9852e006f420c92a7e541b2244a5f Mon Sep 17 00:00:00 2001 From: Zach Pearson Date: Thu, 18 Apr 2024 13:58:37 -0700 Subject: [PATCH 1/2] compat.py: Add utility functions that convert between ints and Qt Enums --- qtpy/compat.py | 24 ++++++++++++++++++++++++ qtpy/tests/test_compat.py | 23 ++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/qtpy/compat.py b/qtpy/compat.py index 4c6d428f..f1a9df88 100644 --- a/qtpy/compat.py +++ b/qtpy/compat.py @@ -5,6 +5,7 @@ """ Compatibility functions """ +import enum import sys from . import ( @@ -200,3 +201,26 @@ def isalive(obj): return shiboken.isValid(obj) return None + + +# ============================================================================= +def getenumasint(enum_value): + """Get the integer value of a Qt enum + For example: + Qt.AlignmentFlag.AlignBaseline -> 256 + Qt.WidgetAttribute.WA_AcceptDrops -> 78 + If an integer is passed in, simply return it. + PySide2's enums are themselves classes, not enum values per se, so if + we get an integer or a class, return the class. + """ + if isinstance(enum_value, enum.Enum): + if PYSIDE2 or PYQT5: + return int(enum_value) + return enum_value.value + return enum_value + + +# ============================================================================= +def getenumfromint(enum_class, i): + """Get the Qt enum value from an integer""" + return enum_class(i) diff --git a/qtpy/tests/test_compat.py b/qtpy/tests/test_compat.py index 1e1fc28f..c2f0f05d 100644 --- a/qtpy/tests/test_compat.py +++ b/qtpy/tests/test_compat.py @@ -1,9 +1,12 @@ """Test the compat module.""" + import sys import pytest -from qtpy import QtWidgets, compat +from packaging import version + +from qtpy import QtWidgets, compat, PYQT5, PYQT_VERSION from qtpy.tests.utils import not_using_conda @@ -22,3 +25,21 @@ def test_isalive(qtbot): with qtbot.waitSignal(test_widget.destroyed): test_widget.deleteLater() assert compat.isalive(test_widget) is False + + +def test_getenumasint(): + """Test compat.getenumasint""" + if PYQT5 and version.parse(PYQT_VERSION) <= version.parse("5.9.2"): + assert compat.getenumasint(QtWidgets.QSizePolicy.Maximum) == 4 + else: + assert compat.getenumasint(QtWidgets.QSizePolicy.Policy.Maximum) == 4 + assert compat.getenumasint(5) == 5 + + +def test_getenumfromint(): + """Test compat.getenumfromint""" + enum_value = compat.getenumfromint(QtWidgets.QSizePolicy.Policy, 7) + if PYQT5 and version.parse(PYQT_VERSION) <= version.parse("5.9.2"): + assert enum_value == QtWidgets.QSizePolicy.Expanding + else: + assert enum_value == QtWidgets.QSizePolicy.Policy.Expanding From 72e9f449b282ed039e44d30f19a5828d18c9b642 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 21 Jul 2024 01:41:35 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- qtpy/tests/test_compat.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qtpy/tests/test_compat.py b/qtpy/tests/test_compat.py index c2f0f05d..b0edb055 100644 --- a/qtpy/tests/test_compat.py +++ b/qtpy/tests/test_compat.py @@ -3,10 +3,9 @@ import sys import pytest - from packaging import version -from qtpy import QtWidgets, compat, PYQT5, PYQT_VERSION +from qtpy import PYQT5, PYQT_VERSION, QtWidgets, compat from qtpy.tests.utils import not_using_conda