3

Is there a way to autosize all fields in an attribute table?

I am working with attribute tables that have over 50 columns and all of them have long names. Instead of autosizing single columns via double-click or right-click...

enter image description here

... I'd rather resize them all at once.

I'd be interested in doing this manually, but a script will do as well.

1 Answer 1

2

Not sure how to achieve this manually, probably a good idea to submit a feature request.

Based on the answer from this post: How to adjust QGIS attribute table view column width using Python?, you could write a script to resize the column width of all loaded attribute tables to fit its contents:

attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName() == u'AttributeTable']

for x in attrTables:
    x.findChildren(QTableView)[0].resizeColumnsToContents()

As mentioned by @BritishSteel in the comment, the above code is for attribute tables opened as a dock window. If the attribute table is either a dock window or a dialog window, the following could be used:

attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName() == u'AttributeTable' or d.objectName().startswith('QgsAttributeTableDialog')]

for x in attrTables:
    x.findChildren(QTableView)[0].resizeColumnsToContents()
3
  • Hmm, in QGIS 3 the attrTables list is empty. Commented Oct 26, 2018 at 13:41
  • 1
    OK, the widget is not called AttributeTable but QgsAttributeTableDialog/nameofatable... so the above list comprehension can be adjusted as follows: attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName().startswith('QgsAttributeTableDialog')] Commented Oct 26, 2018 at 14:26
  • 1
    @BritishSteel - Thanks, I forget I always have the dock window option checked as default. Edited post :)
    – Joseph
    Commented Oct 30, 2018 at 10:42

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.