<div dir="ltr">Thanks Matthias. Got it. I was just trying to not flood messages to those pals who may not be interested.<br><div class="gmail_extra"><br></div><div class="gmail_extra">Best regards,</div><div class="gmail_extra">Ethan<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 13, 2018 at 6:16 PM, Matthias Kuhn <span dir="ltr"><<a href="mailto:matthias@opengis.ch" target="_blank">matthias@opengis.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Glad you like it :)<br>
<br>
Forwading this answer also to the mailing list for future reference,<br>
Ethan, you need to use "reply to all" in your mail application or other<br>
are not able to follow our conversation (which would be a pity ;) ).<br>
<br>
Best regards<br>
Matthias<br>
<span class=""><br>
On 06/13/2018 12:13 PM, Zhang Qun wrote:<br>
> Wow, i was about to look into the with() statement. It looks much<br>
> better. Many thanks for the link. <br>
> <br>
> Regards,<br>
> Ethan<br>
> <br>
> <br>
> On Wed, Jun 13, 2018, 6:03 PM Matthias Kuhn <<a href="mailto:matthias@opengis.ch">matthias@opengis.ch</a><br>
</span><span class="">> <mailto:<a href="mailto:matthias@opengis.ch">matthias@opengis.ch</a>>> wrote:<br>
> <br>
>Â Â Â Hi Ethan,<br>
> <br>
>Â Â Â there are of course advantages and disadvantages of both methods, but in<br>
>Â Â Â most cases the QgsVectorLayer ones just "do the right thing", especially<br>
>Â Â Â when used with a `with` block, see also this post:<br>
> <br>
>Â Â Â <a href="http://www.opengis.ch/2015/08/12/with-edit-layer/" rel="noreferrer" target="_blank">http://www.opengis.ch/2015/08/<wbr>12/with-edit-layer/</a><br>
> <br>
>Â Â Â Cheers<br>
>Â Â Â Matthias<br>
> <br>
>Â Â Â On 06/13/2018 11:59 AM, Zhang Qun wrote:<br>
>Â Â Â > Hi Matthias,<br>
>Â Â Â ><br>
>Â Â Â > Thanks very much for the instruction. I will try it out. The<br>
>Â Â Â > dataProvider method seems simpler to me and it can set attributes in<br>
>Â Â Â > batch as it takes a list as input. The layer method seems only<br>
>Â Â Â takes one<br>
>Â Â Â > input. If i want to change 10 attributes of a feature, i have to issue<br>
>Â Â Â > 10 commands.<br>
>Â Â Â ><br>
>Â Â Â > Best regards,<br>
>Â Â Â > Ethan<br>
>Â Â Â ><br>
>Â Â Â ><br>
>Â Â Â > On Wed, Jun 13, 2018, 5:45 PM Matthias Kuhn <<a href="mailto:matthias@opengis.ch">matthias@opengis.ch</a><br>
>Â Â Â <mailto:<a href="mailto:matthias@opengis.ch">matthias@opengis.ch</a>><br>
</span><div><div class="h5">>Â Â Â > <mailto:<a href="mailto:matthias@opengis.ch">matthias@opengis.ch</a> <mailto:<a href="mailto:matthias@opengis.ch">matthias@opengis.ch</a>>>> wrote:<br>
>Â Â Â ><br>
>Â Â Â >Â Â Â Hi Ethan<br>
>Â Â Â ><br>
>Â Â Â >Â Â Â On 06/13/2018 11:18 AM, Zhang Qun wrote:<br>
>Â Â Â >Â Â Â > Hi everyone, i"m using QGIS2.18, and trying to change feature<br>
>Â Â Â >Â Â Â attributes<br>
>Â Â Â >Â Â Â > using the following two methods:<br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â > *dataProvider:*<br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â > |attrs<br>
>Â Â Â >Â Â Â ={0:"hello",1:123}layer.<wbr>dataProvider().<wbr>changeAttributeValues({fid<br>
>Â Â Â >Â Â Â > :attrs })|<br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â > *layer object:*<br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â <br>
>Â Â Â Â |layer.startEditing()layer.<wbr>changeAttributeValue(fid,<wbr>fieldIndex,value)layer.<wbr>commitChanges()|<br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â > I keep the attribute table open, and monitor the changes.<br>
>Â Â Â The first<br>
>Â Â Â >Â Â Â > method dataProvider is not able to update the attribute<br>
>Â Â Â table on the<br>
>Â Â Â >Â Â Â > fly, even the "reload table" button on the top menu of the<br>
>Â Â Â table does<br>
>Â Â Â >Â Â Â > not work. I have to re-open the table to see the changes.<br>
>Â Â Â The second<br>
>Â Â Â >Â Â Â > method is working, the attribute table gets instantly updated.<br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â > I would like to stay with the dataProvider method but not<br>
>Â Â Â sure how to<br>
>Â Â Â >Â Â Â > get instantly updated attribute table?<br>
>Â Â Â ><br>
>Â Â Â >Â Â Â The provider's dataChanged() signal needs to be emitted, I<br>
>Â Â Â think you can<br>
>Â Â Â >Â Â Â directly emit that or call `dataProvider().forceReload()`<wbr>.<br>
>Â Â Â ><br>
>Â Â Â >Â Â Â Out of curiosity, why would you like to stay with the<br>
>   dataProvider <br>
>Â Â Â >Â Â Â method?<br>
>Â Â Â ><br>
>Â Â Â >Â Â Â Bests<br>
>Â Â Â >Â Â Â Matthias<br>
>Â Â Â ><br>
>Â Â Â ><br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â > Thanks.<br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â > Best regards,<br>
>Â Â Â >Â Â Â > Ethan<br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â > ______________________________<wbr>_________________<br>
>Â Â Â >Â Â Â > QGIS-Developer mailing list<br>
>Â Â Â >Â Â Â > <a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a><br>
>Â Â Â <mailto:<a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.<wbr>osgeo.org</a>><br>
</div></div>>Â Â Â <mailto:<a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.<wbr>osgeo.org</a><br>
<span class="">>Â Â Â <mailto:<a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.<wbr>osgeo.org</a>>><br>
>Â Â Â >Â Â Â > List info:<br>
>Â Â Â <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/<wbr>mailman/listinfo/qgis-<wbr>developer</a><br>
>Â Â Â >Â Â Â > Unsubscribe:<br>
>Â Â Â <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/<wbr>mailman/listinfo/qgis-<wbr>developer</a><br>
>Â Â Â >Â Â Â ><br>
>Â Â Â >Â Â Â _____________________________<wbr>__________________<br>
>Â Â Â >Â Â Â QGIS-Developer mailing list<br>
>Â Â Â >Â Â Â <a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.<wbr>org</a><br>
>Â Â Â <mailto:<a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.<wbr>osgeo.org</a>><br>
</span>>Â Â Â <mailto:<a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.<wbr>osgeo.org</a><br>
<div class="HOEnZb"><div class="h5">>Â Â Â <mailto:<a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.<wbr>osgeo.org</a>>><br>
>Â Â Â >Â Â Â List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/<wbr>mailman/listinfo/qgis-<wbr>developer</a><br>
>Â Â Â >Â Â Â Unsubscribe:<br>
>Â Â Â <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/<wbr>mailman/listinfo/qgis-<wbr>developer</a><br>
>Â Â Â ><br>
> <br>
</div></div></blockquote></div><br></div></div>