0

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++.

niaou suzanne
  • 43
  • 1
  • 9
  • 2
    How about [QTableView::resizeColumnToContents()](https://doc.qt.io/qt-5/qtableview.html#resizeColumnToContents)? – Scheff's Cat Oct 09 '20 at 15:22
  • Yes, but this funciton resize all columns to there contents so even the one which should have been in strech mode adapt to its contents. – niaou suzanne Oct 09 '20 at 16:06
  • The linked function resizes a specific column. (I left out the arg. but just suffixed with `()` to denote that the link goes to a member function.) So, you can decide by yourself (or wrap it into a conditional) if the `resizeColumnToContents()` is applied to a stretchable column or not. – Scheff's Cat Oct 09 '20 at 16:09
  • `void QTableView::resizeColumnToContents(int column)` – Redanium Oct 09 '20 at 16:31
  • I thaught that this function would grow the column in question and push each other following to the right. It is not the case. Thank you ! – niaou suzanne Oct 09 '20 at 16:33

1 Answers1

1

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.

niaou suzanne
  • 43
  • 1
  • 9