2

Is there a Qgs dialog or widget that I can use in a standalone pyqgis application to open the attribute table of a QgsVectorLayer? this seems to be such a simple thing, there has to be an existing widget or dialog...

Update: After some extensive search, there seem to be two ways to do this in pyqgis, neither worked well, I am just going to summarize them here:

Approach 1. use the QgsDualView class as below:

self.editor = QgsDualView(self)
self.editor.init(self.layer, self.map_control.canvas)
self.editor.setView(QgsDualView.AttributeEditor)

This will display attributes as below:

enter image description here

Approach 2. use the QgsAttributeTableView

lyr_cache = QgsVectorLayerCache(self.layer, 120, self)
tbl_model = QgsAttributeTableModel(lyr_cache, self)
tbl_model.loadAttributes()
tbl_model.loadLayer()
tbl_filter_model = QgsAttributeTableFilterModel(self.map_control.canvas, tbl_model, self)
self.editor = QgsAttributeTableView(self)
self.editor.setModel(tbl_filter_model)

This will display as below:

enter image description here

So, as you can see, both look very much half-baked. I did accept @Matthias Kuhn's reply as answer as it did answer my original question, but in light of the fact that it didn't actually work, I continue to hope that someone can give suggestions that could make either approach work better. I will certainly up-vote your answers and give confirmation, this will benefit the larger community.

  • Did you find a solution? I'm trying to display the attribute table too in a standalone application. I'm searching too and I will share my progress if it's successful. – etrimaille Dec 14 '18 at 14:44
2

You can use QgsDualView which is the widget used inside the attribute table dialog in QGIS. The dialog itself (with all the tool buttons on top) is not in a library which is exposed to python.

  • yes, I was able to get it work inside a dialog and display the features, but it only displays feature id, none of the actual feature attributes are displayed. can you give an example on how to get all of the attributes to show up? – For Comment Nov 24 '17 at 7:54
  • I guess you have to call dualview.init( layer, canvas), did you do that? – Matthias Kuhn Nov 24 '17 at 7:56
  • yes I did. It only displayed feature id (the internal id). – For Comment Nov 24 '17 at 7:58
  • Hmm... dualview.setAttributeTableConfig(layer.attributeTableConfig(), does that help? – Matthias Kuhn Nov 24 '17 at 8:29
  • QgsVectorLayer class has no attributeTableConfig method. QgsDualView class doesn't have setAttributeTableConfig method either. I think I know what you mean, I have to set the config to tell it how to display things, but I don't think the table config class is ported to Python yet. – For Comment Nov 24 '17 at 8:37
0

Not sure if this is what you are looking for, but I can access the attributes like this:

# Assuming you have a QgsVectorLayer object called "layer"

# get a list of attributes
fnames = [f.name() for f in layer.fields()]

# print attribute "name" for each feature:
for feat in layer.getFeatures():
    print feat.attribute('name')

# print all attributes for each feature
for feat in layer.getFeatures():
    print feat.attributes()

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.