Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#3713 closed bug (invalid)

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

Reported by: borysiasty Owned by:
Priority: major: does not work as expected Milestone: Version 1.7.0
Component: Vectors Version: Trunk
Keywords: Cc: borysiasty
Must Fix for Release: Yes Platform: All
Platform Version: Awaiting user input: no

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)
  1. The attribute table is empty.
  1. Enter the edit mode
  1. Open the Attribute Table again - now everything works.

Change History (4)

in reply to:  description comment:1 by jef, 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()

comment:2 by borysiasty, 13 years ago

Platform: DebianAll
Resolution: invalid
Status: newclosed

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

comment:3 by wonder, 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

comment:4 by borysiasty, 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.

Note: See TracTickets for help on using tickets.