[GIS] How to connect the selectionChanged signal with pyqt

pythonqgisqgis-plugins

I am developing a plugin for QGIS in Python, I need to connect the sselectionChanged signal emitted when a feature of the layer is selected, I could not find any examples on internet, here is what I have done so far:

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

In the logs of QGIS I get this warning:

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

can you help me?

Best Answer

arpho, this new style PyQt connection works for me in PyQGIS Console (Mac OS X 10.7.3, with QGIS 1.7.4)...

def test(layer): print "selection changed"

qgis.utils.iface.mapCanvas().selectionChanged.connect(test)

I had to hit return at an empty prompt to see the printed "selection changed" messages. If I changed the selection multiple times, those would show up upon hitting return.

The only thing I can see that might cause that error is if your 'self' object hasn't had the iface reference assigned. Is this in your __init__() method of your plugin's class?:

self.iface = iface

Or, that iface hasn't been passed to your plugin's class. Check your __ini__.py file for your plugin to make sure it's being passed as an argument to your plugin's class in the def classFactory() method.

Example:

def classFactory(iface):
    from my_plugin import MyPlugin
    return MyPlugin(iface)