[PyQt] How to get Qt enum class

Phil Thompson phil at riverbankcomputing.com
Tue Sep 2 13:06:54 BST 2014


On 29/08/2014 8:34 am, Florian Bruhin wrote:
> Hi,
> 
> * Phil Thompson <phil at riverbankcomputing.com> [2014-08-14 14:50:18 
> +0100]:
>> On 07/08/2014 7:27 am, Florian Bruhin wrote:
>> >Hi,
>> >
>> >I wrote a function to get the key name in a Qt enum from a value,
>> >intended for debugging mainly. This is how it looks right now:
>> >
>> > <snip>
>> >
>> >Now ideally I'd just pass in the value and nothing else, however I
>> >didn't find a way to get the Qt class where the enum is in, e.g. get
>> >QFont when I have QFont.Sunken:
>> >
>> >>>>QFont.Bold.__class__
>> ><class 'PyQt5.QtGui.Weight'>
>> >>>>QFont.Bold.__class__.__class__
>> ><class 'sip.enumtype'>
>> >
>> >Is there a way I could do this, or do I have to pass this explicitely?
>> 
>> Currently QFont.Weight.__qualname__ returns 'Weight'. Maybe it should 
>> return
>> 'QFont.Weight' instead, the argument being that an enum is a type and 
>> should
>> be treated like a nested class.
>> 
>> Would that solve your problem?
> 
> Hmm. It'd certainly make it a bit easier.

Implemented in tonight's SIP snapshot (for Python v3.3 and later).

> However that sparks an idea: Would it be possible for enum *members*
> to have a __name__ and a __qualname__? Basically like this:
> 
>     >>> QFont.Bold.__name__
>     'Bold'
>     >>> QFont.Bold.__qualname__
>     'QFont.Bold'  # or maybe 'QFont.Weight.Bold'?
> 
> That'd basically do what my qenum_key function does, but without all
> the guesswork.

You should now be able to use...

type(QFont.Bold).__qualname__

Phil


More information about the PyQt mailing list