[GIS] Showing attribute table in form using PyQGIS

attribute-tablepyqgis

Is there any way to show the attribute table of a layer in a QTextBrowser or a QTableView on form?

I did this but, (shows attribute table in a separate dialog) I have no idea, how to show it in my form.

iface.showAttributeTable(iface.activeLayer)

Best Answer

QGIS 2.2 has a table model that you can use for a QTableView. Use it like this:

cache = QgsVectorLayerCache(layer, 10000)
model = QgsAttributeTableModel(cache)
model.loadLayer()
table = QTableView()
table.setModel(model)
Related Question