[Qgis-developer] PyQGIS: Problems with the layerWasAdded SIGNAL

Germán Carrillo carrillo.german at gmail.com
Mon Jun 15 21:55:15 EDT 2009

Hi Martin.

I create the layers like the PyQGIS Wiki says:

( For shapefiles)  layer = QgsVectorLayer( layerPath, layerInfo.fileName(),
layerProvider )
Where layerPath is a QFileDialog result and layerProvider is "ogr".

If the layer is valid, I call a function to add it to canvas:

  if layer.isValid():
    self.agregarCapa( layer )

  def agregarCapa( self, capa ):
    QgsMapLayerRegistry.instance().addMapLayer( capa )
    self.layers.insert( 0, QgsMapCanvasLayer( capa ) )
    self.canvas.setLayerSet( self.layers )

I remove layers from the Legend class:
    for i in self.layers:
      if i.layer().getLayerID() == self.currentItem().layerId:
        self.layers.remove( i )
i.layer().getLayerID() )
        self.canvas.setLayerSet( self.layers )

Then I remove the QTreeWidgetItem:
        self.takeTopLevelItem( self.indexOfTopLevelItem( myItem ) )

Like in the QGIS legend, I have an item for the layer name and its geometry
type and a child item for its symbology.
The parent item (the layer item) has the attribute canvasLayer to reference
it and access its functions and attributes.

I'm using this connect line:
self.connect( QgsMapLayerRegistry.instance(),
SIGNAL("layerWasAdded(QgsMapLayer *)"), self.legend.addLayerToLegend )

The legend has another connect line to update the layer status:
  self.connect( self, SIGNAL("itemChanged(QTreeWidgetItem *,int)"),
self.updateLayerStatus )

When all it's ok, remove layer always returns two lines like this:
Object::disconnect: Unexpected null parameter

Thanks by your help.


El 14 de junio de 2009 5:22, Martin Dobias <wonder.sk at gmail.com> escribió:

> 2009/6/11 Germán Carrillo <carrillo.german at gmail.com>:
> > Hi.
> >
> > I'm using PyQGIS. I have three classes in my app: Viewer, Legend and
> Load.
> > The Viewer class is the main one, the Load class add some data to canvas.
> > The Legend class has a SLOT to capture the layerWasAdded SIGNAL from the
> > QgsMapLayerRegistry class and when it happens add an item in a
> QTreeWidget.
> >
> > The Legend class can remove layers. So, I have a problem with the SIGNAL
> > layerWasAdded. When I remove a layer I get multiple lines like this:
> >
> >   Object::disconnect: Unexpected null parameter
> >
> > After that, if I add the same layer again I get a segmentation fault that
> > crashes my app.
> >
> > This happen even if I use a custom SIGNAL passing the layer object as
> > parameter from my Load class.
> Hi German,
> some code from your app would be helpful. How do you create instances
> of the layer classes? How do you delete the layers?
> Martin


Soluciones Geoinformáticas Libres
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20090615/4db321c8/attachment.html

More information about the Qgis-developer mailing list