tags:

views:

165

answers:

2

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: http://bugreports.qt.nokia.com/browse/QTBUG-9352

A: 

I think that is because QSqlTableModel loads data on demand and the view calculates the column widths based only on data that is available. If you don't need your columns to be user-resizable, you can try this:

tableResult->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
andref
It's not working. The column is still not resizing correctly. I put the code at the end of the function. I've also tried QList<QStringList> as the source of the data with QStandardItemModel as the model.
aurorius
+2  A: 

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);
aurorius