pyqt convert enum value to key

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


Usually, you can do this using the QMetaObject:

m = QtGui.QFrame.staticMetaObject

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

Leave a Reply

Your email address will not be published. Required fields are marked *