Let's assume there is an attribute table of a vector layer which was opened with F6, see image below
With the code below I could set the minimum height and width of all cells in the attribute table.
from PyQt5.QtWidgets import QApplication, QTableView
attrTables = [d for d in QApplication.instance().allWidgets() if 'QgsAttributeTableDialog' in d.objectName()]
if len(attrTables):
attrTables[0].findChildren(QTableView)[0].resizeColumnsToContents()
attrTables[0].findChildren(QTableView)[0].resizeRowsToContents()
However it is only temporal. After closing and opening the table, the height of each cell does not remain, only the width is maintained.
How can I make it permanent in height?
References:
Best Answer
Conclusion
resizeRowsToContents()
works only partially. Because after closing the attribute table, rows won't preserve their dimensionIndeed, as was mentioned in a comment by @Matt, there is a
setAttributeTableConfig()
method of theQgsVectorLayer
class:To get the width of a column one can try the
attributeTableConfig()
method.Nevertheless, it will show the default width which is
-1
.More details can be found in
QgsAttributeTableConfig
class.To change the width of a single column
It utilizes the
setColumnWidth()
andsetAttributeTableConfig()
methodsTo change the width of all columns
Based on @ThomasG77's answer. It uses the
setColumns()
andsetAttributeTableConfig()
methodsKeep in mind, that changes will be noticeable after closing and opening again the attribute table.
However, there are also several incompleteness in the above approaches:
To autosize the width of all columns
Partially overlaps with @Joseph's and @MatthiasKuhn's answers. It applies the
resizeColumnsToContents()
method from theQTableView
class.References: