[Qgis-developer] List layer from Legend

Germán Carrillo carrillo.german at gmail.com
Mon Jan 18 21:10:13 EST 2010


Hi Luiz.

You can get the orderer list of layers from QgsMapCanvas class:

 *canvas = qgis.utils.iface.mapCanvas()
for i in range( canvas.layerCount() ):*

* ly = canvas.layer( i )
*

* print ly.name(), ly.getLayerID()*


I don't know if there's another method but I guess the QgsLegendInterface
class (added in v.1.4) [1] will provide some useful methods to play with
legend layers and groups.

Meanwhile (v.1.4) you can get the group names:

 *legend = qgis.utils.iface.legendInterface()*

*for group in legend.groups():*

* print group*


Germán

-------------------------
[1] http://doc.qgis.org/head/classQgsLegendInterface.html

2010/1/18 Luiz Motta <motta.luiz at gmail.com>

> Hi all,
>
> I am making the python plugin and need show layers in QTreeWidget with same
> order show in Legend/Layer.
>
> The list of layers by :
> layerRegistry = core.QgsMapLayerRegistry.instance()
> it is not same order by Legend.
>
> I have success get list of layer's name by find the QTreeWidget with name
> 'Legend':
> ### getDocWidgetLegend
> def getDocWidgetLegend():
>  widget = None
>  lst = qgis.utils.iface.mainWindow().children()
>  for item in lst:
>   if type(item) == QtGui.QDockWidget and
> item.objectName().compare('Legend') == 0:
>     widget = item
>  return widget
> ### getMapLegend
> def getMapLegend(widgetLegend):
>   widget = None
>  for item in widgetLegend.children():
>   if item.objectName().compare('theMapLegend') == 0:
>     widget =  item
>  return widget
> ### Use functions
> widgetLegend = getDocWidgetLegend()
> treeLegend = getMapLegend(widgetLegend)
> ### End scripts
>
> The problem is when the have same layer's name in Legend, i don't know how
> take exact source.
>
> The other method is read the entries in project:
> project = core.QgsProject.instance()
>
> I don´t know how get entries in tag Legend in project:
> Example QGis project file:
> <!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
> <qgis projectname="" version="1.5.0-Trunk" >
>   <title></title>
>   <mapcanvas>
>       <units>degrees</units>
>       <extent>
>           <xmin>-50.299417</xmin>
>           <ymin>-6.641147</ymin>
>           <xmax>-49.646870</xmax>
>           <ymax>-6.083483</ymax>
>       </extent>
>       <projections>0</projections>
>       <destinationsrs>
>           <spatialrefsys>
>               <proj4>+proj=longlat +ellps=WGS84 +datum=WGS84
> +no_defs</proj4>
>               <srsid>3452</srsid>
>               <srid>4326</srid>
>               <epsg>4326</epsg>
>               <description>WGS 84</description>
>               <projectionacronym>longlat</projectionacronym>
>               <ellipsoidacronym>WGS84</ellipsoidacronym>
>               <geographicflag>true</geographicflag>
>           </spatialrefsys>
>       </destinationsrs>
>   </mapcanvas>
>   <legend>
>       <legendlayer open="true" checked="Qt::Checked"
> name="CB2B-HRC_20091228" >
>           <filegroup open="true" hidden="false" >
>               <legendlayerfile isInOverview="0"
> layerid="CB2B_HRC_2009122820100117201817718" visible="1" />
>           </filegroup>
>       </legendlayer>
> ....
>
> I would like how use "project.entryList()" to take values of  all
> layerid's.
>
> Do someone know how to solve it?
>
> Thank you Luiz
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-- 
-----------
  |\__
(:>__)(
  |/

Soluciones Geoinformáticas Libres
http://geotux.tuxfamily.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20100118/d03381b5/attachment.html


More information about the Qgis-developer mailing list