Search code examples
pythonenumspyqtpyqt6

How is QStyle.StandardPixmap defined in PyQt6


How is the enumeration for QStyle.StandardPixmap defined in PyQt6? I've tried to replicate it as shown below:

from enum import Enum

class bootlegPixmap(Enum):
    SP_TitleBarMenuButton = 0
    SP_TitleBarMinButton = 1
    SP_TitleBarMaxButton = 2

for y in bootlegPixmap:
    print(y)

I get the following output:

bootlegPixmap.SP_TitleBarMenuButton
bootlegPixmap.SP_TitleBarMinButton
bootlegPixmap.SP_TitleBarMaxButton

If I try and iterate over the original using the following code:

from PyQt6.QtWidgets import QStyle
for x in QStyle.StandardPixmap:
    print(x)

I get numerical values only:

0
1
2
...

Solution

  • This has got nothing to do with PyQt per se. It's just the normal behaviour of Python's enum classes.

    By default, the members of IntEnum/IntFlag print their numerical values; so if you want more informative output, you should use repr instead:

    >>> import enum
    >>>
    >>> class X(enum.IntEnum): A = 1
    ...
    >>> f'{X.A} - {X.A!r}'
    1 - <X.A: 1>
    

    By contrast, the members of Enum have opaque values, so they default to a textual representation of the values:

    >>> class Y(enum.Enum): A = 1
    ...
    >>> f'{Y.A} - {Y.A!r}'
    Y.A - <Y.A: 1>
    

    Needless to say, PyQt always uses the integer classes, since that's closest to how they're defined in Qt:

    >>> from PyQt6.QtWidgets import QStyle
    >>>
    >>> QStyle.StandardPixmap.mro()
    [<enum 'StandardPixmap'>, <enum 'IntEnum'>, <class 'int'>, <enum 'ReprEnum'>, <enum 'Enum'>, <class 'object'>]
    >>>
    >>> f'{QStyle.StandardPixmap.SP_ArrowUp} - {QStyle.StandardPixmap.SP_ArrowUp!r}'
    >>> '50 - <StandardPixmap.SP_ArrowUp: 50>'