Auto-resize columns of QTableView programmatically

361 views Asked by At

I have a QTableView in my programm which I want to resize automatically. I used the functions

headerViewHorizontal->setSectionResizeMode(QHeaderView::ResizeToContents);
headerViewHorizontal->setSectionResizeMode(3, QHeaderView::Stretch);

to resize it and it works for when I resize the window. But then I need to insert a row in the table which could need to resize the columns. My problem is here. When I insert a row, one of the fields it too larg and the system write it with tree dots (ex. : "12.03..." instead of "12.03.2020"). I need to resize my window to make the system resize the columns.

Is there a slot which I can call to force an update of the columns width ?

Thank you in advance !

PS I'm using Qt5 with c++.

1

There are 1 answers

0
niaou suzanne On BEST ANSWER

Thanks to @Scheff, my question is answered.

The solution is to use resizeColumnToContent() to one of the columns which has not the stretch resize mode. Each other will be updated.

Edit : This solution works only when the column on which you call resizeColumnToContent() has to change its width. Otherwise you have to call resizeColumnToContent(int column) on each other.