qt-bugs@ issue : None Trolltech task ID : None bugs.kde.org number : None applied: no author: Rafael Fernández López If we have no header, or not visible header on a QTreeView, we can end up with an unusable widget if we expand lots of child widgets of the kind a b c d ... This patch assures that if no header is shown, or if we only have one column (so no other columns become shrinked), the contents will be visible. Index: src/gui/itemviews/qtreeview.h =================================================================== --- src/gui/itemviews/qtreeview.h (revisión: 803370) +++ src/gui/itemviews/qtreeview.h (copia de trabajo) @@ -229,6 +229,7 @@ Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex &, int, int)) Q_PRIVATE_SLOT(d_func(), void _q_columnsRemoved(const QModelIndex &, int, int)) Q_PRIVATE_SLOT(d_func(), void _q_modelAboutToBeReset()) + Q_PRIVATE_SLOT(d_func(), void _q_forceColumnResizeToFitContents()) }; #endif // QT_NO_TREEVIEW Index: src/gui/itemviews/qtreeview.cpp =================================================================== --- src/gui/itemviews/qtreeview.cpp (revisión: 803370) +++ src/gui/itemviews/qtreeview.cpp (copia de trabajo) @@ -246,6 +246,19 @@ connect(d->model, SIGNAL(modelAboutToBeReset()), SLOT(_q_modelAboutToBeReset())); + // we connect these signals from the model to a slot that will call + // resizeColumnToContents. This is important because if we call it only on + // expand() method, when we expand a node, the filling of the model can be + // delayed. So, we call it again after the model has finished its job. + connect(d->model, SIGNAL(layoutChanged()), + this, SLOT(_q_forceColumnResizeToFitContents())); + connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), + this, SLOT(_q_forceColumnResizeToFitContents())); + connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), + this, SLOT(_q_forceColumnResizeToFitContents())); + + + if (d->sortingEnabled) sortByColumn(header()->sortIndicatorSection()); } @@ -2787,6 +2800,8 @@ } if (model->canFetchMore(index)) model->fetchMore(index); + + _q_forceColumnResizeToFitContents(); } void QTreeViewPrivate::collapse(int item, bool emitSignal) @@ -2826,6 +2841,8 @@ else emit q->collapsed(modelIndex); } + + _q_forceColumnResizeToFitContents(); } void QTreeViewPrivate::prepareAnimatedOperation(int item, AnimatedOperation::Type type) @@ -2937,6 +2954,25 @@ viewItems.clear(); } +void QTreeViewPrivate::_q_forceColumnResizeToFitContents() +{ + Q_Q(QTreeView); + + /** + * if: + * + * a) The tree view has no header (user cannot resize the column) OR + * b) The tree view has a header, but hidden (user cannot resize the column) OR + * c) The tree view has a visible header, but with _only_ one (or zero) column (that + * means: no other information will be affected). + * + * We can expand the column to make the contents properly visible. + */ + if (!header || !header->isVisible() || ((header->count() - header->hiddenSectionCount()) <= 1)) { + q->resizeColumnToContents(q->currentIndex().column()); + } +} + void QTreeViewPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end) { Q_UNUSED(parent); Index: src/gui/itemviews/qtreeview_p.h =================================================================== --- src/gui/itemviews/qtreeview_p.h (revision 803534) +++ src/gui/itemviews/qtreeview_p.h (working copy) @@ -101,6 +101,7 @@ QPixmap after; }; + void _q_forceColumnResizeToFitContents(); void expand(int item, bool emitSignal); void collapse(int item, bool emitSignal);