6

Is there a way to hide a column in the attribute table by using PyQGIS?

By hiding I do not mean hiding only the edit widget, I would like to hide the column completely.

It is possible to achieve that by right-clicking on the column in the attribute table and selecting 'Hide column' or 'Organize columns...' but I did not find a way how to do it by using PyQGIS.

Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Oct 29, 2016 at 16:47

2 Answers 2

11

For QGIS versions QGIS 2.16 and higher

Let's define the following handy function:

def setColumnVisibility( layer, columnName, visible ):
 config = layer.attributeTableConfig()
 columns = config.columns()
 for column in columns:
 if column.name == columnName:
 column.hidden = not visible
 break
 config.setColumns( columns )
 layer.setAttributeTableConfig( config )

And then you can call it to hide or show columns in the attribute table. For example:

vLayer = iface.activeLayer()
setColumnVisibility( vLayer, 'FIRST_COLUMN', False ) # Hide FIRST_COLUMN
setColumnVisibility( vLayer, 'area', False ) # Hide area column
setColumnVisibility( vLayer, 'FIRST_COLUMN', True ) # Show FIRST_COLUMN
setColumnVisibility( vLayer, 'area', True ) # Show area column
Taras
35.8k5 gold badges77 silver badges152 bronze badges
answered Oct 31, 2016 at 21:27
3
  • 1
    Nice! Is it possible to refresh/reload an open attribute table without having to close and open it to update changes in the column visibilities? Commented Dec 17, 2020 at 16:01
  • 2
    Unfortunately, I don't see a direct way to do that. Moreover, QgsAttributeTableDialog is not exposed through the API. Commented Dec 20, 2020 at 22:42
  • 1
    I realized opening and closing it is good enough: attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName() == u'QgsAttributeTableDialog' or d.objectName() == u'AttributeTable'] attrTables[0].close() iface.showAttributeTable(lyr) gis.stackexchange.com/questions/231574/… and gis.stackexchange.com/questions/68332/… Commented Dec 21, 2020 at 13:57
4

Another option is to use the setColumnHidden() method of the QgsAttributeTableConfig class.

from qgis.core import QgsProject
def hide_one_column(layer_name: str, column_name: str, column_hidden: bool) -> None:
 """
 Hides or shows a single column in the attribute table of a layer
 Parameters:
 ==========
 :param layer_name: name of the layer
 :param column_name: name of the target column
 :param column_hidden: if the column should be hidden
 """
 
 layer = QgsProject.instance().mapLayersByName(layer_name)[0]
 column_index = layer.fields().indexOf(column_name)
 layer_attr_table_config = layer.attributeTableConfig()
 layer_attr_table_config.setColumnHidden(column_index, column_hidden)
 layer.setAttributeTableConfig(layer_attr_table_config)
 return
hide_one_column('gis_osm_buildings', 'osm_id', True) # Hide "osm_id" field

P.S. To notice applied changes one must close and open the attribute table.

answered Nov 20, 2022 at 11:43
1
  • 1
    This function affects the wrong column, if there are action-columns before. German Carillos solution takes this into account. Commented Mar 6, 2023 at 17:09

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.