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)
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)
| improve this answer | |
  • 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 6: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.

| improve this answer | |
  • The user wants to show the attribute table in their form not a list layers or features. – Nathan W Jun 18 '14 at 11:54

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.