3

I try to create some advanced expression using a QGIS plugin for education rules.

I have a simple Gui :

mygui

My Gui has 7 line edits for text inputs.

I try to using this Expression:

layer = QgsMapLayerRegistry.instance().mapLayersByName("facility")[0] 
request = QgsFeatureRequest().setFilterExpression('"name" =\'{0}\' and "name1" =\'{0}\' and "name2" =\'{0}\' and "name3" =\'{0}\' and "name4" =\'{0}\' and "name5" =\'{0}\' and "name6" =\'{0}\' '.format(lineedit1,lineedit2,lineedit3,lineedit4,lineedit5,lineedit6,lineedit7)) 
for i in layer.getFeatures(request): 
        print I

But if some line edit (it is not necessary to have text in all line edits) is empty then my expression doesn't return some result.

If I try something like this :

if lineedit1 !='' and lineedit2=='' and lineedit3==''and lineedit4==''and lineedit5==''and lineedit6==''and lineedit7=='' :
    layer = QgsMapLayerRegistry.instance().mapLayersByName("facility")[0] 
    request = QgsFeatureRequest().setFilterExpression('"name" =\'{0}\''.format(lineedit1) 
    for i in layer.getFeatures(request): 
            print I

or :

if lineedit1 !='' and lineedit2!='' and lineedit3==''and lineedit4==''and lineedit5==''and lineedit6==''and lineedit7=='' :
    layer = QgsMapLayerRegistry.instance().mapLayersByName("facility")[0] 
    request = QgsFeatureRequest().setFilterExpression('"name" =\'{0}\' and "name" =\'{0}\''.format(lineedit1,lineedit2) 
    for i in layer.getFeatures(request): 
            print I

.....................

then the expression works fine. But I have 7 inputs and many combinations if I continue this example.

Any idea how this expression could work?

1
  • 1
    You are using and to combine your inputs. This constraint will not work when there is an input missing. You could try or to combine the inputs.
    – Stefan
    Mar 15, 2019 at 8:34

0

Your Answer

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