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.

1 Answer 1


You need QGIS >= v2.16 for that.

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
    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
  • 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?
    – BERA
    Dec 17, 2020 at 16:01
  • 1
    Unfortunately, I don't see a direct way to do that. Moreover, QgsAttributeTableDialog is not exposed through the API. Dec 20, 2020 at 22:42
  • 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/…
    – BERA
    Dec 21, 2020 at 13:57

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.