Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
QHeaderView: fix inconsistent saved state, ignored during restore
This happens because QTreeView disconnects QHeaderView's
_q_layoutChanged slot (!). So the stretching of the last section,
done in _q_layoutAboutToBeChanged, invalidated total length, but didn't
recalculate it (since that's done in _q_layoutChanged). As a result,
length was inconsistent, and saveState would save that, and
restoreState() would early-return, not doing anything.

Let's just ensure length is always consistent, so we don't depend on the
complex issue of whether _q_layoutChanged should be called or not.

This an adapted backport of 4a04eea4f4 from 5.11, the unittest shows that
c0e45ae851 is missing in this branch though.

Change-Id: I4137a19e0a6fdf820dd53fb55e858d1d04a2c113
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
  • Loading branch information
dfaure-kdab committed Mar 9, 2018
1 parent 5934f20 commit 4c0a363
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/widgets/itemviews/qheaderview.cpp
Expand Up @@ -2078,7 +2078,11 @@ void QHeaderViewPrivate::_q_layoutAboutToBeChanged()
if (stretchLastSection && lastSectionLogicalIdx >= 0 && lastSectionLogicalIdx < sectionItems.count()) {
const int visual = visualIndex(lastSectionLogicalIdx);
if (visual >= 0 && visual < sectionItems.size()) {
sectionItems[visual].size = lastSectionSize;
auto &itemRef = sectionItems[visual];
if (itemRef.size != lastSectionSize) {
length += lastSectionSize - itemRef.size;
itemRef.size = lastSectionSize;
}
}
}
for (int i = 0; i < sectionItems.size(); ++i) {
Expand Down
53 changes: 53 additions & 0 deletions tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp
Expand Up @@ -162,6 +162,7 @@ private slots:
void renderToPixmap();
void styleOptionViewItem();
void keyboardNavigationWithDisabled();
void saveRestoreState();

// task-specific tests:
void task174627_moveLeftToRoot();
Expand Down Expand Up @@ -4038,6 +4039,58 @@ void tst_QTreeView::keyboardNavigationWithDisabled()
QCOMPARE(view.currentIndex(), model.index(6, 0));
}

class RemoveColumnOne : public QSortFilterProxyModel
{
public:
bool filterAcceptsColumn(int source_column, const QModelIndex &) const override
{
if (m_removeColumn)
return source_column != 1;
return true;
}
void removeColumn()
{
m_removeColumn = true;
invalidate();
}
private:
bool m_removeColumn = false;
};


void tst_QTreeView::saveRestoreState()
{
QStandardItemModel model;
for (int i = 0; i < 100; i++) {
QList<QStandardItem *> items;
items << new QStandardItem(QLatin1String("item ") + QString::number(i)) << new QStandardItem(QStringLiteral("hidden by proxy")) << new QStandardItem(QStringLiteral("hidden by user"));
model.appendRow(items);
}
QCOMPARE(model.columnCount(), 3);

RemoveColumnOne proxy;
proxy.setSourceModel(&model);
QCOMPARE(proxy.columnCount(), 3);

QTreeView view;
view.setModel(&proxy);
view.resize(500, 500);
view.show();
view.header()->hideSection(1); // ## this is wrong, it's 2 in qtbase-5.11
QVERIFY(view.header()->isSectionHidden(1)); // ## this is wrong, it's 2 in qtbase-5.11
proxy.removeColumn();
QCOMPARE(proxy.columnCount(), 2);
QVERIFY(view.header()->isSectionHidden(1));
const QByteArray data = view.header()->saveState();

QTreeView view2;
view2.setModel(&proxy);
view2.resize(500, 500);
view2.show();
view2.header()->restoreState(data);
QVERIFY(view2.header()->isSectionHidden(1));
}

class Model_11466 : public QAbstractItemModel
{
Q_OBJECT
Expand Down

0 comments on commit 4c0a363

Please sign in to comment.