[Qgis-developer] Qgis Api - get a layer by its name

kimaidou kimaidou at gmail.com
Tue Nov 9 11:29:45 EST 2010

Hi Carson,

Thanks for your reply

2010/11/9 Carson Farmer <carson.farmer at gmail.com>

> Hi Kimaidou,
> I suspect that is how most plugin developers have implemented this
> functionality, and for the most part it works (save for the situations
> you listed below).


 One solution would be use userData to store the layers uniqueID, and
> then use that to access layers later on.
> First you add each layer name to the combobox, like so:
> combobox.addItem ( layer.name(),QVariant(layer.getLayerID()))

UniqueId is the best candidate !

> and then when the user select a layer from the combobox, get the
> maplayer by doing something like (untested):
> uniqueId = combobox.itemData(combobox.currentIndex()).toString()
> layer = QgsMapLayerRegistry.instance().mapLayer(uniqueId)


> Of course, if you are simply listing all layers in the layer list,
> then you could just use their order in the combobox, as this will be
> the same as the order in the layer list.
> layer = mapCanvas.layer(combobox.currentIndex())
> Hope that helps,

That helps a lot, thanks !

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20101109/fa113c95/attachment.html

More information about the Qgis-developer mailing list