File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 5
5
"""
6
6
Compatibility functions
7
7
"""
8
+ import enum
8
9
import sys
9
10
10
11
from . import (
@@ -200,3 +201,26 @@ def isalive(obj):
200
201
201
202
return shiboken .isValid (obj )
202
203
return None
204
+
205
+
206
+ # =============================================================================
207
+ def getenumasint (enum_value ):
208
+ """Get the integer value of a Qt enum
209
+ For example:
210
+ Qt.AlignmentFlag.AlignBaseline -> 256
211
+ Qt.WidgetAttribute.WA_AcceptDrops -> 78
212
+ If an integer is passed in, simply return it.
213
+ PySide2's enums are themselves classes, not enum values per se, so if
214
+ we get an integer or a class, return the class.
215
+ """
216
+ if isinstance (enum_value , enum .Enum ):
217
+ if PYSIDE2 or PYSIDE6 :
218
+ return int (enum_value )
219
+ return enum_value .value
220
+ return enum_value
221
+
222
+
223
+ # =============================================================================
224
+ def getenumfromint (enum_class , i ):
225
+ """Get the Qt enum value from an integer"""
226
+ return enum_class (i )
Original file line number Diff line number Diff line change 1
1
"""Test the compat module."""
2
+
2
3
import sys
3
4
4
5
import pytest
@@ -22,3 +23,15 @@ def test_isalive(qtbot):
22
23
with qtbot .waitSignal (test_widget .destroyed ):
23
24
test_widget .deleteLater ()
24
25
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
You can’t perform that action at this time.
0 commit comments