[QGIS-Developer] For an enum in pyqgis, how do I list all members and do reverse lookups?

Julien Cabieces julien.cabieces at oslandia.com
Wed Jul 2 01:00:04 PDT 2025


Hi,

If you want an example, it has been done here: 
https://github.com/qgis/QGIS/blob/cb48529abf5bd81e3c97d09809e3d56e51c943bd/scripts/pyqt5_to_pyqt6/pyqt5_to_pyqt6.py#L796

You have to use __dict__. But I'm not sure this is a good thing to use
this in a plugin. The script I mention is an helper for migration from
qt5 to qt6, so it's kind of special.

Regards,
Julien



> Happy summer to all of you :)
>
> It seems in the Python bindings produced by SIP, a C++ enum is just a Python class with a certain structure. And with little/none possibility of
> introspection.
>
> But how can I get a list with all enum-members (e.g. Symbology, Fields) of that enum?
>
> dir(QgsMapLayer.StyleCategory) does not list them, and QgsMapLayer.StyleCategory.__members__ is undefined. 
>
> Do I really have to hard code the member names into my plugin, if I want to iterate over all of them?
>
> Sincerely, Thomas
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

-- 

Julien Cabieces
Senior Developer at Oslandia
julien.cabieces at oslandia.com


More information about the QGIS-Developer mailing list