Bug report #3713

Memory layer's fields not shown in attribute table (and layer properties) unless refreshed by entering the edit mode

Added by Borys Jurgiel over 13 years ago. Updated over 13 years ago.

Status:Closed
Priority:Low
Assignee:-
Category:Vectors
Affected QGIS version: Regression?:No
Operating System:All Easy fix?:No
Pull Request or Patch supplied: Resolution:invalid
Crashes QGIS or corrupts data: Copied to github as #:13772

Description

If I add a memory layer, it contains features with geometries, but seems the layer doesn't contain any field. In the attribute table the row headers are present, but the column headers are missing, thus the records are empty. The same in Layer properties->Attributes, classification etc. Please note in the python console I can see the fields and attributes.

Now, when I enter the edit mode, something refreshes and everything starts to work properly.

Steps to reproduce:
1. Add a memory layer, e.g from code below or any plugin

layer = [[QgsVectorLayer]]("Point","mylayer","memory")
layer.dataProvider().addAttributes( [QgsField("foo", 2, "Int")] )
f = [[QgsFeature]]()
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(0,0)))
f.setAttributeMap( { 0 : 2 } )
layer.dataProvider().addFeatures( [ f ] )
[[QgsMapLayerRegistry]].instance().addMapLayer(layer)

2. The attribute table is empty.

3. Enter the edit mode

4. Open the Attribute Table again - now everything works.

History

#1 Updated by Jürgen Fischer over 13 years ago

Replying to borysiasty:

Please note in the python console I can see the fields and attributes.

You can only see the fields of the provider. The layer doesn't have any - yet.

But following works fine:

layer = [[QgsVectorLayer]]("Point","mylayer","memory")
[[QgsMapLayerRegistry]].instance().addMapLayer(layer)
layer.startEditing()
layer.addAttribute( [[QgsField]]("foo", 2, "integer", 5) )
f = [[QgsFeature]]()
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(0,0)))
f.setAttributeMap( { 0 : 2 } )
layer.addFeature( f )
layer.commitChanges()

#2 Updated by Borys Jurgiel over 13 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

Ok, it's reasonable. I'll call authors of affected plugins to encapsulate edits in editing mode.

#3 Updated by Martin Dobias over 13 years ago

Borys: even better solution (though available only from 1.7) is to specify the fields when creating the layer, see: http://www.qgis.org/pyqgis-cookbook/vector.html#memory-provider

#4 Updated by Borys Jurgiel over 13 years ago

Yes, but most plugins used to be compatible with older versions.

Btw. a few paragraphs later you give an example that will be affected after adding to the QgsMapLayerRegistry ("The following example code illustrates creating and populating a memory provider"). I'd suggest readers to do vl.startEditing()in advance to avoid a later confusion.

Also available in: Atom PDF