I use this code to initialize QgsAttributeTableView and hide some columns in it in my custom widget:
def __init__(self, layer, canvas, hidden_columns, cache_size=5000, parent=None):
"""Constructor."""
super(SomeWidget, self).__init__(parent)
self.setupUi(self)
self._layer = layer
self._canvas = canvas
self._vector_layer_cache = QgsVectorLayerCache(self._layer, cache_size)
self._attribute_table_model = QgsAttributeTableModel(self._vector_layer_cache)
self._attribute_table_model.loadLayer()
self._attribute_table_filter_model = QgsAttributeTableFilterModel(self._canvas, self._attribute_table_model)
self._attribute_table_view.setModel(self._attribute_table_filter_model)
self.setHidenColumns(hidden_columns)
def setHidenColumns(self, *args):
self._config = self._layer.attributeTableConfig()
columns = self._config.columns()
for column in columns:
if column.name in args:
column.hidden = True
self._config.setColumns(columns)
self._attribute_table_view.setAttributeTableConfig(self._config)
But, no matter what, QgsAttributeTableView shows all the columns in the table. I try to apply self._config to self._layer, then add it to legend, open attribute table and everything works as expected (columns are hidden). That shows me that the config with hidden columns is correct. What is wrong with my approach?