[Qgis-developer] Python Sorting a Layer Selector
Nyall Dawson
nyall.dawson at gmail.com
Sun Jun 5 16:10:04 PDT 2016
On 6 June 2016 at 08:23, Heikki Vesanto <heikki.vesanto at gmail.com> wrote:
> Hi Nyall,
>
> That seems like a great solution. I can get the combo boxes looking as
> expected with ordering an layer type indicators.
Another advantage is that the built in box automatically handles layer
removal and additions, without any extra code required :)
>
> Do you have an example of getting the selection results from that, or
> a plugin that is currently using it?
Use QgsMapLayerComboBox.currentLayer()
(see https://qgis.org/api/classQgsMapLayerComboBox.html#ae2ca2dd3e8cbc81cf0e19244e63efe5a)
Nyall
>
> With the QComboBox I use:
>
> index1 = self.dlg.selectWithCombo.currentIndex()
> selecting_layer = self.dlg.selectWithCombo.itemData(index1)
> index2 = self.dlg.selectFromCombo.currentIndex()
> select_from_layer = self.dlg.selectFromCombo.itemData(index2)
>
> # Either select with all features or just selected ones
> selected_test = self.dlg.selectedFeaturesCheckbox.isChecked()
> if selected_test == 1:
> selecting_feats = selecting_layer.selectedFeatures()
> else:
> selecting_feats_iterator = selecting_layer.getFeatures()
>
> Changing my boxes to QgsMapLayerComboBox results in:
> Traceback (most recent call last):
> File "C:/Users/heikk/.qgis2/python/plugins\SelectWithin\select_within.py",
> line 217, in run
> selecting_feats_iterator = selecting_layer.getFeatures()
> AttributeError: 'NoneType' object has no attribute 'getFeatures'
>
> -Heikki
>
>
> On Sun, Jun 5, 2016 at 9:31 PM, Nyall Dawson <nyall.dawson at gmail.com> wrote:
>>
>> 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
More information about the Qgis-developer
mailing list