Resize of QTableView to have no scrollbars
I have a QTableView on my ui with some columns and rows.
What I would like to achieve is that the TableView resizes to a size that equates the content so that no scrollbars are needed. I hope it's clear what I mean :-)
I have experimented with sizepolicy but with no success.
If it is just that the window the TableView is in be resize, you could set the size of that window. If you don't want the window in which your tableView is display be resized you might want to give the model the notification that it needs to resize its contains. the TableView should have a sizechanged signal, connect that to a slot in the model that calculates the new size per socket/column (e.g. set a smaller / larger font). Then using this value in the model to refresh the tableView (reset or emit dataChanged).
Remember that the tableView isn't responsible for the data that is presented, only to handle the view if needed (e.g. scrollbars). It will get all parameters from the underlying model.
See "QTableView::resizeColumnsToContents()":http://qt-project.org/doc/qt-4.8/qtableview.html#resizeColumnsToContents and "QHeaderView::ResizeMode":http://qt-project.org/doc/qt-4.8/qheaderview.html#ResizeMode-enum.
The resizeColumnsToContents will give the TableView scrollbars if the Table is bigger then the size of the view. Think that he want's to avoid the scrollbar by making the text in the table bigger and smaller to fit in the View.
If the text runs out of the columns as Lukas suggest is the way to go!
[quote author="Jeroentje@home" date="1346847816"]The resizeColumnsToContents will give the TableView scrollbars if the Table is bigger then the size of the view.[/quote]It indeed does, but it allows you to set the preferred width then.
int width = (view.model()->columnCount() - 1) + view.verticalHeader()->width();
for(int column = 0; column < view.model()->columnCount(); column++)
width = width + view.columnWidth(column);