pyqt根据枚举值打印枚举变量名

原文链接:stackoverflow.com

Usually, you can do this using the QMetaObject:
在QT中通常使用QMetaObject和QMetaEnum完成这一操作:

mo = QtGui.QFrame.staticMetaObject
QMetaEnum me = mo.enumerator(mo.indexOfEnumerator('Shadow'))
me.valueToKey(QtGui.QFrame.Sunken)

输出: Sunken

然而,pyqt中的枚举没有暴露元对象,所以必须通过在Python中遍历枚举的属性来做到这一点(幸运的是PyQt枚举是int的子类,因此可以通过isinstance来识别):
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))
                  
print(page_sizes[QtGui.QPrinter.A4])
或 page_sizes[0]
或 page_sizes['A4']

输出: A4
展开阅读全文

没有更多推荐了,返回首页