Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qt

qtableview

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

like image 456
Amree Avatar asked Aug 08 '10 08:08

Amree


3 Answers

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);
like image 160
Amree Avatar answered Nov 09 '22 17:11

Amree


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);
like image 6
andref Avatar answered Nov 09 '22 18:11

andref


I used the same workaround as described by amree, which worked great for the column widths, but tableView->resizeRowsToContents() wasn't working correctly if any offscreen columns had multiline cells that should have caused a row's height to increase.

I looked into the Qt source and it appears that some of the calculations depend on the viewport geometry. This seems to make everything work correctly for both columns and rows:

#include <limits>

tableView->setVisible(false);
QRect vporig = tableView->viewport()->geometry();
QRect vpnew = vporig;
vpnew.setWidth(std::numeric_limits<int>::max());
tableView->viewport()->setGeometry(vpnew);
tableView->resizeColumnsToContents();
tableView->resizeRowsToContents();
tableView->viewport()->setGeometry(vporig);
tableView.setVisible(true);
like image 4
dougg3 Avatar answered Nov 09 '22 17:11

dougg3