Skip to content

Commit b5a8bb6

Browse files
authored
PR: Add QEnum macro for PyQt bindings (#424)
2 parents a5044f2 + 154f6c7 commit b5a8bb6

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

qtpy/QtCore.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
from PyQt5.QtCore import pyqtBoundSignal as SignalInstance
1919
from PyQt5.QtCore import pyqtSlot as Slot
2020
from PyQt5.QtCore import pyqtProperty as Property
21+
try:
22+
from PyQt5.QtCore import Q_ENUM as QEnum
23+
del Q_ENUM
24+
except ImportError: # fallback for Qt5.9
25+
from PyQt5.QtCore import Q_ENUMS as QEnum
26+
del Q_ENUMS
2127
from PyQt5.QtCore import QT_VERSION_STR as __version__
2228

2329
# Those are imported from `import *`
@@ -30,6 +36,7 @@
3036
from PyQt6.QtCore import pyqtBoundSignal as SignalInstance
3137
from PyQt6.QtCore import pyqtSlot as Slot
3238
from PyQt6.QtCore import pyqtProperty as Property
39+
from PyQt6.QtCore import pyqtEnum as QEnum
3340
from PyQt6.QtCore import QT_VERSION_STR as __version__
3441

3542
# For issue #311
@@ -48,7 +55,7 @@
4855
QThread.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
4956

5057
# Those are imported from `import *`
51-
del pyqtSignal, pyqtBoundSignal, pyqtSlot, pyqtProperty, QT_VERSION_STR
58+
del pyqtSignal, pyqtBoundSignal, pyqtSlot, pyqtProperty, pyqtEnum, QT_VERSION_STR
5259

5360
# Allow unscoped access for enums inside the QtCore module
5461
from .enums_compat import promote_enums

qtpy/tests/test_qtcore.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
"""Test QtCore."""
22

3+
import enum
34
import sys
45
from datetime import date, datetime, time
56

67
import pytest
8+
from packaging.version import parse
79

810
from qtpy import (
911
PYQT5,
1012
PYQT6,
1113
PYSIDE2,
12-
PYSIDE6,
1314
PYQT_VERSION,
1415
PYSIDE_VERSION,
1516
QtCore,
1617
)
17-
from qtpy.tests.utils import not_using_conda
1818

1919
_now = datetime.now()
2020
# Make integer milliseconds; `floor` here, don't `round`!
@@ -70,6 +70,23 @@ def test_qthread_exec():
7070
assert QtCore.QThread.exec is not None
7171

7272

73+
@pytest.mark.skipif(PYSIDE2 and parse(PYSIDE_VERSION) < parse("5.15"),
74+
reason="QEnum macro doesn't seem to be present on PySide2 <5.15")
75+
def test_qenum():
76+
"""Test QEnum macro"""
77+
class EnumTest(QtCore.QObject):
78+
class Position(enum.IntEnum):
79+
West = 0
80+
North = 1
81+
South = 2
82+
East = 3
83+
84+
QtCore.QEnum(Position)
85+
86+
obj = EnumTest()
87+
assert obj.metaObject().enumerator(0).name() == "Position"
88+
89+
7390
def test_QLibraryInfo_location_and_path():
7491
"""Test `QLibraryInfo.location` and `QLibraryInfo.path`"""
7592
assert QtCore.QLibraryInfo.location is not None

0 commit comments

Comments
 (0)