[Qgis-developer] problem with feature request and updateFeature

Denis Rouzaud denis.rouzaud at gmail.com
Fri Mar 22 06:43:23 PDT 2013


Hi Matthias,

Thanks for your reply.
This should work better, the loop is not exited.

However, my field values are set to NULL using either 
changeAttributeValue(s) on vector layer or on data provider.

I tried:

vectorLayer.changeAttributeValue( f.id(), fieldIdx, QVariant(alt) )

vectorLayer.dataProvider().changeAttributeValues( {f.id(): {fieldIdx: 
QVariant(alt)}} )

Both return true, but changes are not written. I also tried without 
transforming the values to QVariant.

Do you have any hint?

Thanks,

Denis



On 03/22/2013 02:16 PM, Matthias Kuhn wrote:
> Hi Denis,
>
> On Fre 22 Mär 2013 14:10:19 CET, Denis Rouzaud wrote:
>> Yes exactly
>>
>>              f = QgsFeature()
>>              iter = vectorLayer.getFeatures( QgsFeatureRequest() )
>>              while iter.nextFeature( f ):
>>                  print f.id()
>>                  f.setAttribute( field, QVariant( 1 ) )
>>                  vectorLayer.updateFeature( f )
>>
>> Prints only 1 id, commenting the last line prints all ids.
>>
>> On 03/22/2013 01:56 PM, Nathan Woodrow wrote:
>>> Do you mean your loop is exited when you call updateFeature?
>>>
>>> Can you post your code.
>>>
>>> - Nathan
>>>
>>>
>>> On Fri, Mar 22, 2013 at 10:54 PM, Denis Rouzaud
>>> <denis.rouzaud at gmail.com <mailto:denis.rouzaud at gmail.com>> wrote:
>>>
>>>      Hi all,
>>>
>>>      I am looping over features of a layer using new API
>>>      (QgsFeatureRequest).
>>>      If I modify a feature and do: layer.updateFeature(f), my loop is
>>>      broken.
>>>      Is this a normal behavior?
>>>
>>>      What should I do if I want to modify features during the loop?
>>>
>>>      Thanks
>>>
>>>      Denis
>>>      _______________________________________________
>>>      Qgis-developer mailing list
>>>      Qgis-developer at lists.osgeo.org
>>>      <mailto:Qgis-developer at lists.osgeo.org>
>>>      http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>
>>>
>>
>>
>> _______________________________________________
>> Qgis-developer mailing list
>> Qgis-developer at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
> updateFeature invokes a new feature request inside, so I guess, this is
> the reason for your request being closed. So in order to be able to use
> this method in a loop, multiple concurrent threads should be allowed to
> work on a dataprovider (which is not supported by any yet as far as I
> know)
>
> Try using changeAttributeValue / changeGeometry instead. It's faster
> anyway.
>
> Cheers,
> Matthias



More information about the Qgis-developer mailing list