1

The code written by Joseph answering to the question Features to stay selected by default in QGIS?

layer = qgis.utils.iface.activeLayer()
# Use active layer

def select(featureAdded):
    layer.setSelectedFeatures([featureAdded])

layer.featureAdded.connect(select)
# Connect "featureAdded" event to "select" function

works, but as soon as another feature is created, a new selection is made, resulting in only the new feature been selected.

I would like to create many features that stay selected, and that are added to the features already selected. I am not good enough with scripting to create a working code.

1

if you want add each new feature to selection , need add QgsVectorLayer.AddToSelection

Sample:

layer = iface.activeLayer()
def select(featureAdded):
    print(featureAdded)
    layer.selectByIds([featureAdded],QgsVectorLayer.AddToSelection)

layer.featureAdded.connect(select)

tested in QGIS 3.4

0

Inside the select() function, you just need to replace :

layer.setSelectedFeatures([featureAdded])

with:

layer.select(featureAdded)

So the code would look like:

layer = iface.activeLayer()
def select(featureAdded):
    layer.select(featureAdded)

layer.featureAdded.connect(select)

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.