My question is somehow related to the problem of finding the id of the last added feature: I am looking for a way to supply the feature id and another attribute of each added feature through a signal. The user can change many layers, but only in case of changing a special layer this signal should be emitted/evaluated.

The solution to the question mentioned above is to connect to the featureAdded signal of the layer being edited.

Instead of editing layers I am going to add features to the dataProvider. In this case the signal is not emitted through the layer.

I see three ideas to prove:

  1. Adding an appropriate signal to dataProvider objects (my preferred solution)

  2. Changing layers featureAdded signal to include feature id and the other attribute (second best solution), and fire it manually.

  3. Define an application specific signal and emit this whenever a feature is added to any provider (the easiest way, but filtering signals by this special layer is required)

In all cases one question is: how can I get the feature id of the feature being added to a dataProvider? According to the first and second idea: is it possible to extend the contents of a signal, or to bind one more signal to mapLayer/dataProvider objects?

  • Do you still need a solution for this question? Jul 24 '16 at 12:49
  • Yes. My workaround is based on the assumption, that feature ids are incremented by 1. For the use case in mind this holds, but nevertheless a general solution is appreciated.
    – Detlev
    Jul 25 '16 at 19:26

If it is not absolutely necessary for your code to catch a signal...

You can use the vectorLayer.dataProvider.addFeatures() method to assign the new feature(s) to a variable. Once caught in the variable, iterate through them and call the id() function.

See my answer to a similar question.


Can you use the "python Init Function" of the layer ? From the example in the tooltip:

def my_form_open(dialog,layer,feature):
     geom = feture.geometry()
     id = feature.id()
  • When I create a QgsFeature() and add it to the dataprovider() with addFeatures() its feature id is equal to 0. It gets its id after being added. Once added to layer or dataProvider it is another feature. In this case this feature has to be queried after being added to determine what the id is. I want to push this information to other modules, in order to let them react to the change.
    – Detlev
    Mar 9 '16 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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