Skip to content

Commit 2207ae0

Browse files
committed
compat.py: Add utility functions that convert between ints and Qt Enums
1 parent 3238de7 commit 2207ae0

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

qtpy/compat.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,22 @@ def isalive(obj):
200200

201201
return shiboken.isValid(obj)
202202
return None
203+
204+
205+
# =============================================================================
206+
def getenumasint(enum_value):
207+
"""Get the integer value of a Qt enum
208+
For example:
209+
Qt.AlignmentFlag.AlignBaseline -> 256
210+
Qt.WidgetAttribute.WA_AcceptDrops -> 78
211+
If an integer is passed in, simply return it.
212+
"""
213+
if isinstance(enum_value, int):
214+
return enum_value
215+
return enum_value.value
216+
217+
218+
# =============================================================================
219+
def getenumfromint(enum_class, i):
220+
"""Get the Qt enum value from an integer"""
221+
return enum_class(i)

qtpy/tests/test_compat.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test the compat module."""
2+
23
import sys
34

45
import pytest
@@ -22,3 +23,15 @@ def test_isalive(qtbot):
2223
with qtbot.waitSignal(test_widget.destroyed):
2324
test_widget.deleteLater()
2425
assert compat.isalive(test_widget) is False
26+
27+
28+
def test_getenumasint():
29+
"""Test compat.getenumasint"""
30+
assert compat.getenumasint(QtWidgets.QSizePolicy.Policy.Maximum) == 4
31+
assert compat.getenumasint(5) == 5
32+
33+
34+
def test_getenumfromint():
35+
"""Test compat.getenumfromint"""
36+
enum_value = compat.getenumfromint(QtWidgets.QSizePolicy.Policy, 7)
37+
assert enum_value == QtWidgets.QSizePolicy.Policy.Expanding

0 commit comments

Comments
 (0)