3

I'm setting up a custom widget in a QGIS3 plugin and include a QgsAttributeTableView like this:

layer = iface.activeLayer()
layer.getFeatures()

canvas = iface.mapCanvas()
vector_layer_cache = QgsVectorLayerCache(layer, 10000)
attribute_table_model = QgsAttributeTableModel(vector_layer_cache)
attribute_table_model.loadLayer()

attribute_table_filter_model = QgsAttributeTableFilterModel(canvas, attribute_table_model)
self.attribute_table_view = QgsAttributeTableView()
self.attribute_table_view.setModel(attribute_table_filter_model)

print("GIS-Layer: "+str(layer.allFeatureIds()))

self.layTab.addWidget(self.attribute_table_view)

But no data is shown in the QgsAttributeTableView. The print-output displays the list of all feature-ids of the loaded layer. So I think there are features, but not shown in my QgsAttributeTableView.

1 Answer 1

1

This works OK for me. It's possible that your issue is attribute_table_model going out of scope and being deleted, leaving no model to power the table view. But that's tricky to diagnose without seeing more of your code. I'd try changing attribute_table_model to be a member of self, just like your view is.

1
  • @ ndawson: Thank you for your hint, now it works. But I had to change 'vector_layer_cache' and 'attribute_table_filter_model' also to be a member of 'self'!
    – vezzon
    Commented Jan 17, 2019 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.