[Qgis-developer] Python Sorting a Layer Selector

Nyall Dawson nyall.dawson at gmail.com
Sun Jun 5 13:31:30 PDT 2016


On 6 Jun 2016 6:28 AM, "Heikki Vesanto" <heikki.vesanto at gmail.com> wrote:
>
> 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.

Why not use QgsMapLayerComboBox instead? This has sorting built in, and
will help your plugin match the standard QGIS layer selector behaviour.

Nyall
>
> -Heikki
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160606/ad98373e/attachment.html>


More information about the Qgis-developer mailing list