14

Is it possible to open an attribute table from the Python Console or through a Python script in QGIS?

Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Aug 10, 2013 at 13:52

1 Answer 1

17

Yes you can, at least with the latest master version and 1.8. It is a method exposed via iface

// C++ (signature)
void showAttributeTable (QgsVectorLayer *l)
# PyQGIS (example)
iface.showAttributeTable(iface.activeLayer())

You will need a preexisting reference to a QgsVectorLayer.

For direct editing of features it is also a method exposed via iface:

// C++ (signature)
bool openFeatureForm (QgsVectorLayer *l, QgsFeature &f, bool updateFeatureOnly=false)
# PyQGIS (example)
iface.openFeatureForm(iface.activeLayer(), feature, False)

You will need a preexisting reference to a QgsVectorLayer and a QgsFeature in both cases. The feature form opened and whether it is editable are dependent upon app settings and whether the feature's layer is in edit mode.

ThomasG77
31.7k1 gold badge56 silver badges96 bronze badges
answered Aug 10, 2013 at 22:07
4
  • The Python example worked well for me when in Python console. However, I tried to run this script in Processing, where the layer = processing.getObject() of a loaded layer. From iface.showAttributeTable(layer), I got Name Error: global name 'iface' is not defined. Any suggestions? Commented Apr 1, 2014 at 23:31
  • @dakcarto You need to import iface with 'from qgis.utils import iface' Commented Nov 25, 2014 at 1:33
  • @user25976 The reference to iface only works for the console or scripts run from within QGIS, including plugins like Processing. For the console, it is auto-imported for convenience, with all other cases needing the import of qgis.utils. This is described at the beginning of the PyQGIS cookbook. You can not access iface in a standalone app, since the Python modules do not instantiate the QGIS desktop GUI application. Commented Nov 25, 2014 at 18:27
  • 1
    @dakcarto iface.openFeatureForm(iface.activeLayer(), feature, False) This is useful when we want to edit the selected feature but if i want to add a new feature how can i do that? without feature Commented Feb 18, 2015 at 13:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.