aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/doc/extras/QtCore.QFlag.rst
blob: dd4f028006f3c226e5b4fb8069f89eaa2a669658 (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
.. currentmodule:: PySide6.QtCore
.. py:decorator:: QFlag

QFlag handles 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>`_.

Example
-------

::

    from enum import Flag, auto

    from PySide6.QtCore import QFlag, QObject

    class Demo(QObject):

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


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):
        ...

See :deco:`QEnum` for registering Python Enum derived classes.

Meanwhile all enums and flags have been converted to Python Enums
(default since ``PySide 6.4``), see the :ref:`NewEnumSystem` section.