How to make sure columns in QTableView are resized to the maximum

Amree picture Amree · Aug 8, 2010 · Viewed 46.4k times · Source

I'm not sure how to ask this, so, feel free to ask for more information.

It seems that tableView->resizeColumnsToContents() will only resize all the columns based on data in current view. Which means that if I have more data below (which is longer in terms of counts of words), those words will be wrapped down (if the wordWrap property is set to true).

The weird thing is, if I scroll down to the bottom and refresh the data, tableView will resize those columns correctly. It seems as if tableView didn't know there are longer text below.

So, my question is, how can I make sure those columns are resized to the max based on all of the data?

My codes

QSqlTableModel *model = new QSqlTableModel;
model->setTable("item");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();    
tableResult->setModel(model);    
tableResult->setEditTriggers(QAbstractItemView::NoEditTriggers);    
tableResult->setSelectionBehavior(QAbstractItemView::SelectRows);
tableResult->setSelectionMode(QAbstractItemView::SingleSelection);    
tableResult->resizeColumnsToContents();
tableResult->resizeRowsToContents();

Update 1

I've tried tableResult->scrollToBottom() and it will only resize based on items at the bottom. So, if there are longer words in the middle, those words will get wrapped.

Update 2

If anyone would like to understand what I'm talking about, just download this example. You'll see that clicking the PushButton will generate a data that's not resized correctly.

Update 3

Possibly a bug: https://bugreports.qt.io/browse/QTBUG-9352

Answer

Amree picture Amree · Aug 10, 2010

I managed to find a workaround for this issue, you just need to hide the table before calling resizeColumnsToContents().

For an example:

tableResult->setVisible(false);
tableResult->resizeColumnsToContents();
tableResult->setVisible(true);