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
78.6k57 gold badges260 silver badges407 bronze badges
asked Jun 18, 2014 at 5:03

2 Answers 2

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)
answered Jun 18, 2014 at 11:57
2
  • 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. Commented Jun 19, 2014 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() Commented Aug 25, 2016 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.

answered Jun 18, 2014 at 8:03
1
  • The user wants to show the attribute table in their form not a list layers or features. Commented Jun 18, 2014 at 11:54

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.