Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm tring to programmatically create layer sequence also using groups. Simple python code:

layer = QgsVectorLayer("004cp000.shp", "004cp000", "ogr")
legend = qgis.utils.iface.legendInterface()
index = legend.addGroup ("G1")
index = legend.addGroup ("G2",index)
legend.moveLayer (legend.layers()[0],index)
  • How it is possible to remove a nested group ? qgis.utils.iface.legendInterface().removeGroup only works with top level groups.

  • How a layer can be moved into a nested group? qgis.utils.iface.legendInterface().moveLayer only works with top level groups.

  • How can I move a layer before or after a another layer inside the same group?

After some research into QGis Source code I've discovered that it is not possible to move a layer into a sub group and also it is not possible to delete a soub group.

void QgsLegend::moveLayer( QgsMapLayer *ml, int groupIndex )
  if ( !ml )

  QgsLegendLayer *layer = findLegendLayer( ml->id() );
  if ( !layer )

  QgsLegendGroup *group = dynamic_cast<QgsLegendGroup*>( topLevelItem( groupIndex ) );
  if ( !group )

  insertItem( layer, group );

  emit itemMovedGroup( dynamic_cast<QgsLegendItem*>( layer ), groupIndex );

void QgsLegend::removeGroup( int groupIndex )
  QgsLegendGroup * lg = dynamic_cast<QgsLegendGroup *>( topLevelItem( groupIndex ) );
  if ( lg )
    removeGroup( lg );
share|improve this question
I'm from Argentina (my english is very bad!!). I'm developer in Avenue (ArcView 3.1) and recently i'm begining to learn how to programming in Phyton for QGis. Your information is very good!!! Thanks you!! –  user27940 Mar 11 at 17:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.