QTreeView: recalculate row heights in hide/showColumn()

When calling QTreeView::hideColumn() the row heights are not
recalculated. This can lead to rows which are unnecessarily high due to
hidden columns may contain large (e.g. multiline) content. The same
applies to showColumn() - there the row might be to small.
Hiding columns directly via QTreeView::header()->hideSection() is not
covered by this patch since QHeaderView has no way to inform about
newly shown/hidden sections.

Task-number: QTBUG-8376
Change-Id: I20b1198e56e403ab8cf649af76e5e2280821dd68
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Christian Ehrlicher 2017-11-12 13:33:25 +01:00
parent 5c0a9fc532
commit ba2f3a156e
2 changed files with 53 additions and 0 deletions

View File

@ -736,7 +736,10 @@ void QTreeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &botto
void QTreeView::hideColumn(int column)
{
Q_D(QTreeView);
if (d->header->isSectionHidden(column))
return;
d->header->hideSection(column);
doItemsLayout();
}
/*!
@ -747,7 +750,10 @@ void QTreeView::hideColumn(int column)
void QTreeView::showColumn(int column)
{
Q_D(QTreeView);
if (!d->header->isSectionHidden(column))
return;
d->header->showSection(column);
doItemsLayout();
}
/*!

View File

@ -193,6 +193,7 @@ private slots:
void taskQTBUG_37813_crash();
void taskQTBUG_45697_crash();
void taskQTBUG_7232_AllowUserToControlSingleStep();
void taskQTBUG_8376();
void testInitialFocus();
};
@ -4399,5 +4400,51 @@ void tst_QTreeView::taskQTBUG_7232_AllowUserToControlSingleStep()
QCOMPARE(hStep1, t.horizontalScrollBar()->singleStep());
}
static void fillModeltaskQTBUG_8376(QAbstractItemModel &model)
{
model.insertRow(0);
model.insertColumn(0);
model.insertColumn(1);
QModelIndex index = model.index(0, 0);
model.setData(index, "Level0");
{
model.insertRow(0, index);
model.insertRow(1, index);
model.insertColumn(0, index);
model.insertColumn(1, index);
QModelIndex idx;
idx = model.index(0, 0, index);
model.setData(idx, "Level1");
idx = model.index(0, 1, index);
model.setData(idx, "very\nvery\nhigh\ncell");
}
}
void tst_QTreeView::taskQTBUG_8376()
{
QTreeView tv;
QStandardItemModel model;
fillModeltaskQTBUG_8376(model);
tv.setModel(&model);
tv.expandAll(); // init layout
QModelIndex idxLvl0 = model.index(0, 0);
QModelIndex idxLvl1 = model.index(0, 1, idxLvl0);
const int rowHeightLvl0 = tv.rowHeight(idxLvl0);
const int rowHeightLvl1Visible = tv.rowHeight(idxLvl1);
QVERIFY(rowHeightLvl0 < rowHeightLvl1Visible);
tv.hideColumn(1);
const int rowHeightLvl1Hidden = tv.rowHeight(idxLvl1);
QCOMPARE(rowHeightLvl0, rowHeightLvl1Hidden);
tv.showColumn(1);
const int rowHeightLvl1Visible2 = tv.rowHeight(idxLvl1);
QCOMPARE(rowHeightLvl1Visible, rowHeightLvl1Visible2);
}
QTEST_MAIN(tst_QTreeView)
#include "tst_qtreeview.moc"