Skip to content Skip to sidebar Skip to footer

Pyqt Allowed Enumeration Values And Strings

In PySide I can get the dictionary with possible/allowed enumerator values and their string representations by using values attribute. For example: QtWidgets.QMessageBox.StandardBu

Solution 1:

PySide has a built-in enum type (Shiboken.EnumType) which supports iteration over the names/values. It also supports a name attribute, which you can use to get the enumerator name directly from its value.

Unfortunately, PyQt has never had these features, so you will have to roll your own solution. It's tempting to use QMetaType for this, but some classes don't have the necessary staticMetaObject. In particular, the Qt namespace doesn't have one, which rules out using QMetaType for a very large group of enums.

So a more general solution would be to use python's dir function to build a two-way mapping, like this:

defenum_mapping(cls, enum):
    mapping = {}
    for key indir(cls):
        value = getattr(cls, key)
        ifisinstance(value, enum):
            mapping[key] = value
            mapping[value] = key
    return mapping

enum = enum_mapping(QMessageBox, QMessageBox.StandardButton)

print('Ok = %s' % enum['Ok'])
print('QMessageBox.Ok = %s' % enum[QMessageBox.Ok])
print('1024 = %s' % enum[1024])
print()

for item insorted(enum.items(), key=str):
    print('%s = %s' % item)

Output:

Ok = 1024QMessageBox.Ok = Ok
1024 = Ok

Abort = 262144Apply = 33554432ButtonMask = -769Cancel = 4194304Close = 2097152Default = 256Discard = 8388608Escape = 512FirstButton = 1024FlagMask = 768Help = 16777216Ignore = 1048576LastButton = 134217728No = 65536NoAll = 131072NoButton = 0NoToAll = 131072Ok = 1024Open = 8192Reset = 67108864RestoreDefaults = 134217728Retry = 524288Save = 2048SaveAll = 4096Yes = 16384YesAll = 32768YesToAll = 32768-769 = ButtonMask
0 = NoButton
1024 = Ok
1048576 = Ignore
131072 = NoToAll
134217728 = RestoreDefaults
16384 = Yes16777216 = Help
2048 = Save
2097152 = Close
256 = Default
262144 = Abort
32768 = YesToAll
33554432 = Apply
4096 = SaveAll
4194304 = Cancel
512 = Escape
524288 = Retry
65536 = No67108864 = Reset
768 = FlagMask
8192 = Open
8388608 = Discard

Post a Comment for "Pyqt Allowed Enumeration Values And Strings"