I have an application in PyQt that displays data in a
The problem with this and something that I did not like is that the second column does not stretch, does not fit the content. Well, I thought it would be easy to fix that, Qt should have something to solve me this problem:
It works! It was what he wanted :).
Then came the real problem. I tried to resize the columns with the mouse, and did not work :(.
I spent hours looking for the solution on some websites said he had to reimplement methods
QHeaderView and stuff, but I thought there must be something easier. After a long time, I really was much !, I found something that I solved that damn problem:
def resizeEvent(self, event): """ Resize all sections to content and user interactive """ super(Table, self).resizeEvent(event) header = self.horizontalHeader() for column in range(header.count()): header.setSectionResizeMode(column, QHeaderView.ResizeToContents) width = header.sectionSize(column) header.setSectionResizeMode(column, QHeaderView.Interactive) header.resizeSection(column, width)
That was all!
QTableView, then each section resizes according to the content and get the width of this, finally I tell the header I want to interact with it and give the width you got. Thus, the width of the header is set to content and can interact with it in order to change the size if you wish.