2

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 21, 2019 at 16:28

2 Answers 2

3

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

answered Mar 22, 2019 at 11:14
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)
answered Mar 22, 2019 at 11:01

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.