2

Is there a way to open attribute table at set the setting to "show selected features" through pyqgis? enter image description here

  • For QGIS 2.x or 3.0? – ndawson May 23 '18 at 23:41
3

Yes, you can change the setting (which is available in Settings > Options > Data Sources > Attribute table behaviour) and set this to show selected features whenever the table opens:

from PyQt4.QtCore import QSettings
QSettings().setValue("/Qgis/attributeTableBehaviour", "1")

Attribute table behaviour


Edit:

Using iface.showAttributeTable(iface.activeLayer()) seems to ignore the QSetting and instead show all features as default. An alternative to load the attribute table of the selected layer with the chosen QSetting could be the following:

from PyQt4.QtGui import QAction
iface.mainWindow().findChild( QAction, 'mActionOpenTable' ).trigger()
|improve this answer|||||
  • 1
    And to open attribute table with pyqgis see this q/a, gis.stackexchange.com/questions/68332/… – artwork21 May 23 '18 at 12:23
  • Thanks, this works when i right click and opens attribute table, but not if i use iface.showAttributeTable(iface.activeLayer()). Do you know how I can get it to work with that? – Kasper Skjeggestad Oct 11 '18 at 11:03
  • @KasperSkjeggestad - Edited post :) – Joseph Oct 11 '18 at 11:27
  • @KasperSkjeggestad - Most welcome, glad it helped :) – Joseph Oct 11 '18 at 12:48
  • Please if your plugin change QSettings in the background, you should restore the previous value after that. I would hate a plugin changing my settings everytime I'm using it. Prefer the solution where you do not alter user settings. – etrimaille Jun 4 '19 at 18:59
4

This is building on the accepted answer by Joseph but I don't have a enough reputation to comment on that. Anyone else searching for this may find the following useful to open an attribute table and then set its filter mode without needing to change the QSettings:

attDialog=iface.showAttributeTable(iface.activeLayer())  #Or specify a QgsVectorLayer 

attDialog.findChild(QAction,'mActionSelectedFilter').trigger()
|improve this answer|||||

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.