I had to fix the same problem. I ended up implementing a function to compute the minimum width of a list view, and then constraining the view using setFixedWidth(myComputedWidth).
Here is the code of my function:
int minWidth = 0;
// Too bad view->viewOptions() is protected
// FIXME: Handle the case where text is below icon
option.decorationSize = view->iconSize();
int rowCount = model->rowCount();
for (int row = 0; row<rowCount; ++row) {
QSize size
= view
->itemDelegate
()->sizeHint
(option, index
);
minWidth = qMax(size.width(), minWidth);
}
minWidth += 2 * view->frameWidth();
return minWidth;
}
int computeListViewMinimumWidth(QAbstractItemView* view) {
int minWidth = 0;
QAbstractItemModel* model = view->model();
// Too bad view->viewOptions() is protected
// FIXME: Handle the case where text is below icon
QStyleOptionViewItem option;
option.decorationSize = view->iconSize();
int rowCount = model->rowCount();
for (int row = 0; row<rowCount; ++row) {
QModelIndex index = model->index(row, 0);
QSize size = view->itemDelegate()->sizeHint(option, index);
minWidth = qMax(size.width(), minWidth);
}
minWidth += 2 * view->frameWidth();
return minWidth;
}
To copy to clipboard, switch view to plain text mode
Bookmarks