aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/doc/extras/QtCore.QEnum.rst
blob: d331342646e69703451b6bdc61fb0a358d709d5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
.. currentmodule:: PySide6.QtCore
.. _QEnum:

QEnum/QFlag
***********

This class decorator is equivalent to the `Q_ENUM` macro from Qt.
The decorator is used to register an Enum to the meta-object system,
which is available via `QObject.staticMetaObject`.
The enumerator must be in a QObject derived class to be registered.


Example
-------

::

    from enum import Enum, Flag, auto

    from PySide6.QtCore import QEnum, QFlag, QObject

    class Demo(QObject):

        @QEnum
        class Orientation(Enum):
            North, East, South, West = range(4)

        class Color(Flag):
            RED = auto()
            BLUE = auto()
            GREEN = auto()
            WHITE = RED | BLUE | GREEN

        QFlag(Color)    # identical to @QFlag usage


Caution:
--------

QEnum registers a Python Enum derived class.
QFlag treats a variation of the Python Enum, the Flag class.

Please do not confuse that with the Qt QFlags concept. Python does
not use that concept, it has its own class hierarchy, instead.
For more details, see the `Python enum documentation <https://docs.python.org/3/library/enum.html>`_.


Details about Qt Flags:
-----------------------

There are some small differences between Qt flags and Python flags.
In Qt, we have for instance these declarations:

::

    enum    QtGui::RenderHint { Antialiasing, TextAntialiasing, SmoothPixmapTransform,
                                HighQualityAntialiasing, NonCosmeticDefaultPen }
    flags   QtGui::RenderHints

The equivalent Python notation would look like this:

::

    @QFlag
    class RenderHints(enum.Flag)
        Antialiasing = auto()
        TextAntialiasing = auto()
        SmoothPixmapTransform = auto()
        HighQualityAntialiasing = auto()
        NonCosmeticDefaultPen = auto()


As another example, the Qt::AlignmentFlag flag has 'AlignmentFlag' as the enum
name, but 'Alignment' as the type name. Non flag enums have the same type and
enum names.

::

    enum Qt::AlignmentFlag
    flags Qt::Alignment

The Python way to specify this would be

::

    @QFlag
    class Alignment(enum.Flag):
        ...

We are considering to map all builtin enums and flags to Python enums as well
in a later release.