3

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)
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Dil
  • 143
  • 1
  • 12

2 Answers2

4

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)
Nathan W
  • 34,706
  • 5
  • 97
  • 148
  • Thank You for the answer. I tried this but it shows nothing. I'm very new to qgis and python,so I don't know what should I do to show the attribute table in my QTableView. Please help me to do it. – Dil Jun 19 '14 at 06:15
  • QGIS 2.16 crashes when trying to apply this to a plugin tableview. But it only crashes when I try to use .show() – Barbarossa Aug 25 '16 at 20:33
-2

I am using a button to get current layers from canvas in my qgis2leaf plugin like this:

canvas = qgis.utils.iface.mapCanvas()
        allLayers = canvas.layers()
        for i in allLayers:
            if i.type() == 2:
                print(i.name() + " skipped as it is not a vector layer nor a raster layer")  
            if i.type() < 2: 
                self.ui.listWidget.addItem(i.name())

what you can do: do another loop over the layer and get all attributes. with

attributes = i.getFeatures()

and print them in another listwidget in your GUI.

Riccardo
  • 2,648
  • 18
  • 31