[Qgis-developer] QgsFieldExpressionWidget return the results of the expression

Rouzaud Denis denis.rouzaud at gmail.com
Tue May 10 12:05:35 PDT 2016


HI Matteo,


> On 10 May 2016, at 18:14, matteo <matteo.ghetta at gmail.com> wrote:
> 
> Hi devs,
> 
> I'm struggling with a maybe simple issue..
> 
> In a plugin I have added a QgsFieldExpressionWidget connected to a QgsMapLayerComboBox..
> 
> What I am currently trying to do without any result is to get a list of the field values filtered with the result of the expression added
> 
> In a *normal* QgsFieldComboBox I achive it as follows:
> 
> # QgsMapLayerComboBox
> lay1 = self.Field1.layer()
> # QgsFieldComboBox
> lay1_f = self.Field1.currentField()
> 
> 
> # create the list of the selected fields
> f1 = []
>    for i in lay1.getFeatures():
>    f1.append(i[lay1_f])

You’re doing feature[field_name] which is fine, but doing feature[expression] is not valid because there is no index corresponding.
You need to evaluate the expression.

So basically, just test if currentField returns an expression or not.
If not, keep your line.
If it is, evaluate the expression 
https://qgis.org/api/classQgsExpression.html#a279785f0a59cc4ac977022797599bdc6 <https://qgis.org/api/classQgsExpression.html#a279785f0a59cc4ac977022797599bdc6>

I hope this helps!

Denis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160510/9ccf90b7/attachment.html>


More information about the Qgis-developer mailing list