5

I have created a script which opens the attribute table in QGIS and I would like to use the advanced filter (expression). I want to create the expression from python and then show the result in the attribute table.

Code:

request = QgsFeatureRequest().setFilterExpression( u'"STATUS" = \'OK\'' )
self.attDialog = self.iface.showAttributeTable(self.layer)
self.attDialog.findChild(QAction,'mActionAdvancedFilter').trigger()

Do you know how could I do that?

The expected result should be something like this: enter image description here

  • Do you get any error when using the script below? – Kadir Şahbaz Feb 17 at 13:56
  • Nop. If you want to try it in the console, remove the 'self.' and assign layer = iface.activeLayer() – p374 Feb 17 at 14:18
  • Where do you find 'mActionFieldFilter' ? – Vincent Bré Feb 17 at 14:25
  • Sorry, its AdvancedFilter, just edited it @VincentBré – p374 Feb 17 at 14:27
  • it does open a modal expression builder dialog when I try it on the console, confirm? (appologize my quick shot answer below...) – Jochen Schwarze Feb 17 at 14:55
6

QGIS Python Editor version:

from qgis.PyQt.QtGui import *

layer = iface.activeLayer()

attDialog = iface.showAttributeTable(layer)

# firstly, you need get some widgets
mFilterButton = attDialog.findChild(QToolButton,'mFilterButton')
mActionAdvancedFilter = attDialog.findChild(QAction,'mActionAdvancedFilter')
mFilterQuery = attDialog.findChild(QLineEdit,'mFilterQuery')
mActionApplyFilter = attDialog.findChild(QAction,'mActionApplyFilter')

# set checked Advanced Filter (Expression)
mFilterButton.setDefaultAction(mActionAdvancedFilter)

# You can use string version of request
request = u'"STATUS" = \'OK\'' 
# add query string to the text box
mFilterQuery.setText(request)
mFilterQuery.setVisible(True)

# apply query
mActionApplyFilter.trigger()

enter image description here

Plugin version:

self.attDialog = self.iface.showAttributeTable(self.layer)

mFilterButton = self.attDialog.findChild(QToolButton,'mFilterButton')
mActionAdvancedFilter = self.attDialog.findChild(QAction,'mActionAdvancedFilter')
mFilterQuery = self.attDialog.findChild(QLineEdit,'mFilterQuery')
mActionApplyFilter = self.attDialog.findChild(QAction,'mActionApplyFilter')

...
...
|improve this answer|||||
  • Amazing! This was exactly what I was looking for! Thank you very much! – p374 Feb 17 at 22:21
5

Either

You can pass the expression string directly to the selectByExpression() method of your QgsVectorLayer instance like this:

self.layer.selectByExpression('"STATUS" = \'OK\'')

u'...' is not needed since everything is unicode in Python 3.

Then you show the attribute table as shown above

self.attDialog = self.iface.showAttributeTable(self.layer)

and set its behaviour to only show the selected features

self.attDialog.findChild(QAction,'mActionSelectedFilter').trigger()

cp. PyQGIS Attribute table show selected features

Or

If working with selections is unwanted one may apply a filter to the layer by

self.layer.setSubsetString(''"STATUS" = \'OK\''')

and open the attribute table.

Delete the filter by

self.layer.setSubsetString('')

Note that subset strings are plain sql.

cp. https://qgis.org/api/classQgsVectorLayer.html#aba0ee124dcf2d037f3af53d99866c01c

|improve this answer|||||
  • I don't want to use 'mActionSelectedFilter' because if I select one of the selected features the rest of them will disappear from the attribute table. – p374 Feb 17 at 14:21
  • got you. see my edit, possibly applying a filter is what you are looking for. – Jochen Schwarze Feb 17 at 14:38
  • 1
    Your subsetString thing works fine but only if the layer is NOT in edit mode! – p374 Feb 17 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.