您需要做的是每次选择图层时创建一个信号:
def initGui(self):
# ... your other code here
self.handler = None
self.selected_layer = None
QObject.connect(self.iface,SIGNAL("currentLayerChanged(QgsMapLayer *)") ,self.layerChanged)
def layerChanged(self, layer):
if self.handler:
QObject.disconnect(self.selected_layer, SIGNAL("selectionChanged()"),self.select_changed)
self.handler = False
self.selected_layer = None
if layer is not None:
if layer.isValid():
QObject.connect(layer,SIGNAL("selectionChanged()"),self.select_changed)
self.selected_layer = layer
def select_changed(self):
# whatever
pass
编辑:断开连接(错误层)修复