Setting size of QTableView columns to fit the contents programatically
-
wrote on 9 Jan 2021, 19:05 last edited by
Hi,
I'm trying to set columns width of QTableView to fit displayed contents.
Using QHeaderView::ResizeToContents is not an option, because the model displayed by QTableView represent quite big data on remote server. QHeaderView::ResizeToContents causes all data is read first to get the biggest width, which is something to be avoided (otherwise I could use QTableWidget).
Instead I'm using a proxy where I monitor data passing thru and resize the column when neccesary. The problem is, that I don't know how to get the proper width. I tried
QFontMetrics{ tableWidget->font() }.horizontalAdvance(str)
and
QFontMetrics{ tableWidget->font() }.boundingRect(str).width()
and then I call
tableWidget->horizontalHeader()->resizeSection()
but the value from font metrics is not big enough and the string still doesn't fit into the cell.
Thanks for any help
Pavel
-
You have to add also some margins left and right of the text.
1/2