Saturday, 25 March 2017

pyqgis - How to adjust QGIS attribute table view column width using Python?


I'm programmatically opening the Attribute table via Python through a plugin. All of the columns have a default width, but some of the data that I'm using do not fit. Default column width


It's not enough to be able to click the column separator to make the column re-size to fit the data as there are several columns and several layers.


Can I, through Python, change this in any way, or is my only option to create my own table in Qt?



Answer



from PyQt4.QtGui import QApplication, QTableView


# Get a list of all open attribute table dialogs
attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName() == u'QgsAttributeTableDialog' or d.objectName() == u'AttributeTable' ]
# Set the column width for the first column in the first dialog to 200
attrTables[0].findChildren(QTableView)[0].setColumnWidth( 0, 200 )
# or instead set the width to match the contents maximum size
# attrTables[0].findChildren(QTableView)[0].resizeColumnsToContents()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...