[Qgis-developer] Python Sorting a Layer Selector

Heikki Vesanto heikki.vesanto at gmail.com
Sun Jun 5 13:27:59 PDT 2016


Hey,

I had a feature request through for a python plugin to sort layer
drop-down lists alphabetically:

https://github.com/HeikkiVesanto/QGIS_Centroid_Within/issues/3

This is a list of vector layers that are open, that are selected using
a QComboBox QT drop-down. I get the layers using:

layers = QgsMapLayerRegistry.instance().mapLayers().values()

Before adding them to the QT dialog:

for layer in layers:
            if layer.type() == QgsMapLayer.VectorLayer:
                self.dlg.selectWithCombo.addItem( layer.name(), layer )
                self.dlg.selectFromCombo.addItem( layer.name(), layer )

I would like to sort these layers based on their name, so layer.name().

I got as far as sorting by name:
layers.sort(key=operator.methodcaller("name"), reverse=False)

However this is case sensitive, with upper case appearing first. Does
anyone know of a plugin that implements sorting? Or an easy way to do
it. If all else fails I can revert back to case sensitive sorting.

-Heikki


More information about the Qgis-developer mailing list