2

I need to run a for loop to select features by their attributes, because the values are within lists (strkey and strvalue). I'm using the following script:

k=0
v=0
layer = iface.activeLayer()
for item in keys:
 listk = strkey[k]
 listv = strvalue[v]
 layer.selectByExpression('\"LINE_ID\" IN ('+listk+') AND \"Declividad\" IN ('+listv+')', QgsVectorLayer.SetSelection)
 v=v+1
 k=k+1

The problem is that only the last feature is selected. Is like every loop the Qgis erases the old selection to select the new feature. Is there a way to accumulate the selections ?

asked Jun 17, 2019 at 19:39

1 Answer 1

4

The reason why your selections are being overwritten on each iteration is because you passed QgsVectorLayer.SetSelection for the SelectBehaviour parameter. This sets a new selection each time, removing any existing selection.

Changing your SelectBehaviour argument to QgsVectorLayer.AddToSelection should do the trick.

answered Jun 17, 2019 at 22:36
1
  • simple like that! thanks! Do you have a good source so I could study more about this? Commented Jun 18, 2019 at 12:27

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.