[QGIS-Developer] Get data from QgsFieldExpressionWidget (QGIS Master)

Denis Rouzaud denis.rouzaud at gmail.com
Mon Jun 26 04:31:04 PDT 2017

Hi Matteo,

Le lun. 26 juin 2017 à 13:11, matteo <matteo.ghetta at gmail.com> a écrit :

> Hi all,
> I'm writing a plugin that uses QgsFieldExpressionWidget to get the field
> and its attributes from a layer.
> I'm really fighting to find a method to get the attributes of the field
> as a list when an expression if typed.

It's a bit unclear to me the distinction you make between fields and
attributes. Can you detail what you call "attributes of the field"?

> Supposing the layer has a field called PH and n attributes:
> 1. if just PH (so the name of the field) is selected than it is pretty
> straightforward:
> # self.expBox is the name of the widget
> field = self.expBox.currentText()
> data = []
> for i in vl.getFeatures():
>     data.append(i[field])
> 2. if a whatever expression is typed, e.g. PH > 7, than I get some
> problem on how I can retrieve the name of the field from the widget.

Well, you don't have a field anymore as you typed an expression. There
might be ways to get fields out of an expression I guess. Not sure it's in
the public API though. Better ask experts here.

> Knowing the field name it should be something like:
> exp = QgsFeatureRequest().setFilterExpression(' PH > 7 ')
> request.setSubsetOfAttributes([])
> data = []
> for f in vl.getFeatures(request):
>     data.append(f['PH'])
> so I don't understand how I can get just the PH string (so the field
> name) from the widget in order to put it in the loop and get only the
> required attributes .

 So, if I understand well you want to retrieve the fields used in an
expression, is this correct?
Matthias or others can answer this for sure.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20170626/e78acde9/attachment.html>

More information about the QGIS-Developer mailing list