Is it possible to convert a qt enum value to its key?
For example I’d like to get to get ‘A4’ from QPrinter class knowing that QPrinter.A4 = 0
Thank you in advance
Answer
Usually, you can do this using the QMetaObject
:
m = QtGui.QFrame.staticMetaObject m.enumerator(m.indexOfEnumerator('Shadow')).valueToKey(QtGui.QFrame.Sunken) 'Sunken'
However it appears that QPrinter
hasn’t exposed a meta object, so you’ll have to do it by walking QPrinter
‘s attributes in Python (fortunately PyQt enums are subclasses of int
, so can be identified by isinstance
):
page_sizes = dict((n, x) for x, n in vars(QtGui.QPrinter).items() if isinstance(n, QtGui.QPrinter.PageSize)) page_sizes[QtGui.QPrinter.A4] 'A4'