diff options
author | David Faure <david.faure@kdab.com> | 2018-03-08 22:31:33 +0100 |
---|---|---|
committer | David Faure <david.faure@kdab.com> | 2018-03-09 18:43:29 +0000 |
commit | 4c0a363a9a0b73c68fd252cba705396d4378b209 (patch) | |
tree | 997ef2f7a3dd1f16f67f5927bdbcaa482a988478 /tests | |
parent | 5934f209c06bf622b6dc321de9a4a7b3dd6b0dda (diff) |
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>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp index dfcaa9b5b9..6ecee806de 100644 --- a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp +++ b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp @@ -162,6 +162,7 @@ private slots: void renderToPixmap(); void styleOptionViewItem(); void keyboardNavigationWithDisabled(); + void saveRestoreState(); // task-specific tests: void task174627_moveLeftToRoot(); @@ -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 |