dataProvider信号dataChange仅工作一次

时间:2019-06-25 18:30:21

标签: python pyqt4 qgis

将功能添加到我的层时,我试图更新表,我正在尝试使用来自该层的dataProvider的dataChanged信号。我能够一次更新表格,但是此后不会触发信号。我创建了一个简单的python代码示例来测试该问题:

qgis_ltr_dir = "C:/Program Files/QGIS 2.18/apps/qgis-ltr"

import sys
from qgis.gui import *
from qgis.core import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *

def func():
    print 'Print' 

app = QApplication(sys.argv)

QgsApplication.setPrefixPath(qgis_ltr_dir, True)
QgsApplication.initQgis()

vector_layer = QgsVectorLayer("/shapefiles/my_shapes.shp", "vector_layer", "ogr")
print vector_layer.isValid()

QgsMapLayerRegistry.instance().addMapLayer(vector_layer)

#vector_layer.featureAdded.connect(func)
vector_layer.dataProvider().dataChanged.connect(func)

print 'Before First Add'
new_feat = QgsFeature(vector_layer.fields())
vector_layer.dataProvider().addFeatures([new_feat])

print 'Before Second Add'
new_feat2 = QgsFeature(vector_layer.fields())
vector_layer.dataProvider().addFeatures([new_feat2])

代码输出为:

True
Before First Add
Print
Before Second Add

“ True”表示已正确加载该图层,“ Print”应打印两次,但在这种情况下应打印一次。

我已经尝试过QgsVectorLayer的featureAdd,但是信号根本没有发出。

链接到信号文档:https://qgis.org/pyqgis/master/core/QgsDataProvider.html#qgis.core.QgsDataProvider.dataChanged

0 个答案:

没有答案