1

嗨,我正在为 python 中的 Qgis 开发一个插件,我需要连接选择图层功能时发出的 sselectionChanged 信号,我在互联网上找不到任何示例,这是我目前所做的

QObject.connect(self.iface.mapCanvas(),SIGNAL("selectionChanged(QgsMapLayer)"), self.test)

在 qgis 的日志中,我收到此警告

Warning: Object::connect:  (receiver name: 'MainWindow')
4

1 回答 1

2

您需要做的是每次选择图层时创建一个信号:

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

编辑:断开连接(错误层)修复

于 2012-06-03T15:06:26.837 回答