2

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 added 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'

I can't find a lot of examples of implementing signals on QGIS objects.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 13, 2012 at 18:04

1 Answer 1

7

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.

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
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Aug 13, 2012 at 19:30
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.