Skip to content

Commit

Permalink
compat.py: Add utility functions that convert between ints and Qt Enums
Browse files Browse the repository at this point in the history
  • Loading branch information
zjp committed Apr 19, 2024
1 parent 3238de7 commit 2207ae0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions qtpy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,22 @@ 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.
"""
if isinstance(enum_value, int):
return enum_value
return enum_value.value


# =============================================================================
def getenumfromint(enum_class, i):
"""Get the Qt enum value from an integer"""
return enum_class(i)
13 changes: 13 additions & 0 deletions qtpy/tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the compat module."""

import sys

import pytest
Expand All @@ -22,3 +23,15 @@ 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"""
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)
assert enum_value == QtWidgets.QSizePolicy.Policy.Expanding

0 comments on commit 2207ae0

Please sign in to comment.