QTreeView: Too many columns. What to do?
-
My tree view has too many columns, so the user would have to scroll sideways to view them all. My idea is to group columns together, so the user can hide groups and show them again. Has anyone ever tried this? I'm not sure how to visualize the groups and add a button (or similar) to collapse them. Does anyone know an easy way to do this?
-
Proxy models are what you need.
QSortFilterProxyModel should suit your needs to filter (or sort if ever needed) your columns.
However this won't be enough for grouping, I think you will have to inherit QAbstractProxyModel.
If you want to have a button or so that would allow for hide/show columns, you simply connect the button to the appropriate slot in the proxy model.
-
@ankou29666 said in QTreeView: Too many columns. What to do?:
QSortFilterProxyModel should suit your needs to filter (or sort if ever needed) your columns.
The OP appears (to me?) to want to hide columns. How would
QSortFilterProxyModel
ever hide a complete column (as opposed to showing the column with blank values in it)?I agree about proxy model which removes undesired columns, but I don't see
QSortFilterProxyModel
doing that (it can remove rows but not columns)? -
@JonB i think it's the way I did it some time ago. But you're making me doubt. As far as I remember I did it (but with table model&view, not with tree !!!, this is maybe where I'm going wrong ?) with QSortFilterProxyModel and I think it was using filterAcceptColumn.
-
@JonB See QSortFilterProxyModel::filterAcceptsColumn() but tbh I never tested or used it :)
-
@ankou29666 , @Christian-Ehrlicher
You are both quite correct! I had never seen that, I thought it only hadfilterAcceptsRow()
, it can do even more than I realised, my bad! -
I lean toward QTreeView::setColumnHidden() rather than QSortFilterProxyModel::filterAcceptsColumn(), but either will work. Using a filter proxy will require a call to QSortFilterProxyModel::invalidate() or QSortFilterProxyModel::invalidateFilter() each time columns should be shown or hidden. Showing and hiding in the view rather than via a model might be more efficient.
As for how to trigger it,