如何获得selectionChanged信号

时间:2012-04-14 19:49:21

标签: plugins pyqt qgis

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

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

在qgis的日志中我得到了这个警告

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

1 个答案:

答案 0 :(得分:1)

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

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

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