Hi Ethan,

there are of course advantages and disadvantages of both methods, but in
most cases the QgsVectorLayer ones just "do the right thing", especially
when used with a `with` block, see also this post:

http://www.opengis.ch/2015/08/12/with-edit-layer/

Cheers
Matthias

On 06/13/2018 11:59 AM, Zhang Qun wrote:
> Hi Matthias,
> 
> Thanks very much for the instruction. I will try it out. The
> dataProvider method seems simpler to me and it can set attributes in
> batch as it takes a list as input. The layer method seems only takes one
> input. If i want to change 10 attributes of a feature, i have to issue
> 10 commands.
> 
> Best regards,
> Ethan
> 
> 
> On Wed, Jun 13, 2018, 5:45 PM Matthias Kuhn <[email protected]
> <mailto:[email protected]>> wrote:
> 
>     Hi Ethan
> 
>     On 06/13/2018 11:18 AM, Zhang Qun wrote:
>     > Hi everyone, i"m using QGIS2.18, and trying to change feature
>     attributes
>     > using the following two methods:
>     >
>     > *dataProvider:*
>     >
>     > |attrs
>     ={0:"hello",1:123}layer.dataProvider().changeAttributeValues({fid
>     > :attrs })|
>     >
>     > *layer object:*
>     >
>     >
>     
> |layer.startEditing()layer.changeAttributeValue(fid,fieldIndex,value)layer.commitChanges()|
>     >
>     > I keep the attribute table open, and monitor the changes. The first
>     > method dataProvider is not able to update the attribute table on the
>     > fly, even the "reload table" button on the top menu of the table does
>     > not work. I have to re-open the table to see the changes. The second
>     > method is working, the attribute table gets instantly updated.
>     >
>     > I would like to stay with the dataProvider method but not sure how to
>     > get instantly updated attribute table?
> 
>     The provider's dataChanged() signal needs to be emitted, I think you can
>     directly emit that or call `dataProvider().forceReload()`.
> 
>     Out of curiosity, why would you like to stay with the dataProvider 
>     method?
> 
>     Bests
>     Matthias
> 
> 
>     >
>     > Thanks.
>     >
>     > Best regards,
>     > Ethan
>     >
>     >
>     > _______________________________________________
>     > QGIS-Developer mailing list
>     > [email protected] <mailto:[email protected]>
>     > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>     > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>     >
>     _______________________________________________
>     QGIS-Developer mailing list
>     [email protected] <mailto:[email protected]>
>     List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>     Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> 
_______________________________________________
QGIS-Developer mailing list
[email protected]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to