我在一个矢量层上工作,我必须将所有实体id属性合并到实体(N)id中,其中实体( n+i )id等于实体(N)id,然后删除所有n+i实体。一切都很好,但我在提交更改之前调用了几次startEditing函数,我的问题是:调用commitChanges是关闭startEditing,还是让它打开,就像它是一个文件描述符或一个我们需要在作业完成后释放的指针?
代码是:
olayer.startEditing()
olayer.changeAttributeValue(n,id_obj,id_obj_sum,NULL,True)
olayer.commitChanges()
olayer.startEditing()
i= i-1
while i >=1:
olayer.deleteFeature(n+i)
i=i-1
olayer.commitChanges()如你所见,我们调用了几次olayer.startEditing,甚至更多,因为所有的代码都在while body中...那么,是否会产生大量startEditing“指针”,或者只是不断地将较老的可编辑状态设置为“开放编辑”?
实际上代码可以工作,但是它慢得让人痛苦,这是原因吗?
发布于 2021-06-22 01:32:02
一般来说,您不应在图层中多次启动编辑模式。
您可以在对层的所有更改结束时提交,以便您的修改在此期间保留在编辑缓冲区中。
如果您传递一个False作为参数(该参数称为stopEditing,请参见the docs),则QgsVectorLayer.commitChanges()可以打开编辑模式。如下所示:
# If the commit is sucessful, let the edit mode open
layer.commitChanges(False) 另外,看看QGIS docs中的with edit(layer):语法。使用这样的语法可以避免启动/提交/关闭编辑模式,QGIS会为您做这件事。
with edit(layer):
# All my layer modifications here
# When this line is reached, my layer modifications are already committed.https://stackoverflow.com/questions/64713921
复制相似问题