File tree Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 55"""
66Compatibility functions
77"""
8+ import enum
89import sys
910
1011from . import (
@@ -200,3 +201,26 @@ def isalive(obj):
200201
201202 return shiboken .isValid (obj )
202203 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 PYQT5 :
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 11"""Test the compat module."""
2+
23import sys
34
45import pytest
56
6- from qtpy import QtWidgets , compat
7+ from packaging import version
8+
9+ from qtpy import QtWidgets , compat , PYQT5 , PYQT_VERSION
710from qtpy .tests .utils import not_using_conda
811
912
@@ -22,3 +25,21 @@ def test_isalive(qtbot):
2225 with qtbot .waitSignal (test_widget .destroyed ):
2326 test_widget .deleteLater ()
2427 assert compat .isalive (test_widget ) is False
28+
29+
30+ def test_getenumasint ():
31+ """Test compat.getenumasint"""
32+ if PYQT5 and version .parse (PYQT_VERSION ) <= version .parse ("5.9.2" ):
33+ assert compat .getenumasint (QtWidgets .QSizePolicy .Maximum ) == 4
34+ else :
35+ assert compat .getenumasint (QtWidgets .QSizePolicy .Policy .Maximum ) == 4
36+ assert compat .getenumasint (5 ) == 5
37+
38+
39+ def test_getenumfromint ():
40+ """Test compat.getenumfromint"""
41+ enum_value = compat .getenumfromint (QtWidgets .QSizePolicy .Policy , 7 )
42+ if PYQT5 and version .parse (PYQT_VERSION ) <= version .parse ("5.9.2" ):
43+ assert enum_value == QtWidgets .QSizePolicy .Expanding
44+ else :
45+ assert enum_value == QtWidgets .QSizePolicy .Policy .Expanding
You can’t perform that action at this time.
0 commit comments