Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've created a test plug-in in QGIS successfully, but have had a hard time implementing a signal on a vector layer event (add or modify feature). I've add this to my initGUI method:

QObject.connect(self.iface.activeLayer(),SIGNAL("featureAdded(QgsFeatureId)"),self.addedGeometry)

and this definition:

def addedGeometry(self, intValue):
        QMessageBox.information( self.iface.mainWindow(),"Info", "Hi!!!" )

I get the following error when I load my plug-in:

QObject.connect(self.iface.activeLayer(),SIGNAL("featureAdded(QgsFeatureId)"),self.addedGeometry)
TypeError: arguments did not match any overloaded call:
  QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'NoneType'
  QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): first argument of unbound method must have type 'QObject'

If anyone could help I would be very grateful! I can't find a lot of examples of implementing signals on QGIS objects.

share|improve this question

1 Answer 1

self.iface.activeLayer() is relative to what layer is selected in the legend. If no layer is selected it returns 0 (not a QObject and resolved to 'NoneType'), which can not accept a signal/slot connection.

You'll need to do two things before building the signal/slot connection:

  1. Verify that self.iface.activeLayer() is in fact returning a QgsMapLayer and not 0
  2. Verify the active layer's LayerType is a QgsVectorLayer, which produces the featureAdded(QgsFeatureId) signal you're looking to connect to.

Edit: there is good information for writing plugins here:

Also, consider using new style PyQt connection syntax (here, once you have defined a variable 'vlayer' for the vector layer):

vlayer.featureAdded.connect(self.addedGeometry)

@pyqtSlot(int)
def addedGeometry(self, intValue):
    # fun stuff here
share|improve this answer
    
Thank you! I realized this since I posted, but have had more problems. I checked this: self.iface.mapCanvas().layerCount() and it returns 0 in my initGui method so i can't get any layer here. i think i have my signal binding in the wrong location. any advice on where to move it to? thank you, i'm very new to qGIS development and just trying it out to see if we can replace some of ESRI development w/ qGIS. –  user9515 Aug 13 '12 at 19:32
    
Not sure without seeing the actual code, but it sounds like you are trying to set up your plugin's functional signal/slot connections on plugin load (i.e. during initGui()). Depending upon your plugin's function this may be the correct place for some connections. If your plugin is working with an activeLayer() you will probably want to make that connection in your plugin's class, if GUI-based, or as a conditional part of a slot connected to the self.iface.currentLayerChanged() signal. –  dakcarto Aug 13 '12 at 19:52
    
thank you. this helps. i think i need to change the approach and the structure of the tool. –  user9515 Aug 13 '12 at 20:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.