summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@trolltech.com>2009-11-23 10:59:05 (GMT)
committerOlivier Goffart <ogoffart@trolltech.com>2009-11-23 11:14:11 (GMT)
commitb4dbbd30c8092a81984cca30248db087353fe66e (patch)
tree7d338a46623118bf262fe58ac5731ddf040a5ced
parent76b66d6da548ce4a247727f14e09b1d301330db5 (diff)
downloadQt-b4dbbd30c8092a81984cca30248db087353fe66e.zip
Qt-b4dbbd30c8092a81984cca30248db087353fe66e.tar.gz
Qt-b4dbbd30c8092a81984cca30248db087353fe66e.tar.bz2
Fix QHeaderView when the model is reset and section have moved.
The logicalIndices and visualIndices array where not clean of the old sections, resulting in corrupted header. Task-number: QTBUG-6058 Reviewed-by: Thierry
-rw-r--r--src/gui/itemviews/qheaderview.cpp25
-rw-r--r--tests/auto/qheaderview/tst_qheaderview.cpp46
2 files changed, 65 insertions, 6 deletions
diff --git a/src/gui/itemviews/qheaderview.cpp b/src/gui/itemviews/qheaderview.cpp
index 5df8481..6f0fba6 100644
--- a/src/gui/itemviews/qheaderview.cpp
+++ b/src/gui/itemviews/qheaderview.cpp
@@ -1913,7 +1913,6 @@ void QHeaderView::initializeSections(int start, int end)
Q_ASSERT(start >= 0);
Q_ASSERT(end >= 0);
- d->executePostedLayout();
d->invalidateCachedSizeHint();
if (end + 1 < d->sectionCount) {
@@ -1939,11 +1938,25 @@ void QHeaderView::initializeSections(int start, int end)
d->sectionCount = end + 1;
if (!d->logicalIndices.isEmpty()) {
- d->logicalIndices.resize(d->sectionCount);
- d->visualIndices.resize(d->sectionCount);
- for (int i = start; i < d->sectionCount; ++i){
- d->logicalIndices[i] = i;
- d->visualIndices[i] = i;
+ if (oldCount <= d->sectionCount) {
+ d->logicalIndices.resize(d->sectionCount);
+ d->visualIndices.resize(d->sectionCount);
+ for (int i = oldCount; i < d->sectionCount; ++i) {
+ d->logicalIndices[i] = i;
+ d->visualIndices[i] = i;
+ }
+ } else {
+ int j = 0;
+ for (int i = 0; i < oldCount; ++i) {
+ int v = d->logicalIndices.at(i);
+ if (v < d->sectionCount) {
+ d->logicalIndices[j] = v;
+ d->visualIndices[v] = j;
+ j++;
+ }
+ }
+ d->logicalIndices.resize(d->sectionCount);
+ d->visualIndices.resize(d->sectionCount);
}
}
diff --git a/tests/auto/qheaderview/tst_qheaderview.cpp b/tests/auto/qheaderview/tst_qheaderview.cpp
index c13e829..a8e7461 100644
--- a/tests/auto/qheaderview/tst_qheaderview.cpp
+++ b/tests/auto/qheaderview/tst_qheaderview.cpp
@@ -43,6 +43,7 @@
#include <QtTest/QtTest>
#include <QStandardItemModel>
#include <QStringListModel>
+#include <QSortFilterProxyModel>
#include <qabstractitemmodel.h>
#include <qapplication.h>
@@ -188,6 +189,7 @@ private slots:
void task236450_hidden_data();
void task236450_hidden();
void task248050_hideRow();
+ void QTBUG6058_reset();
protected:
QHeaderView *view;
@@ -1947,5 +1949,49 @@ void tst_QHeaderView::task248050_hideRow()
}
+//returns 0 if everything is fine.
+static int checkHeaderViewOrder(QHeaderView *view, const QVector<int> &expected)
+{
+ if (view->count() != expected.count())
+ return 1;
+
+ for (int i = 0; i < expected.count(); i++) {
+ if (view->logicalIndex(i) != expected.at(i))
+ return i + 10;
+ if (view->visualIndex(expected.at(i)) != i)
+ return i + 100;
+ }
+ return 0;
+}
+
+
+void tst_QHeaderView::QTBUG6058_reset()
+{
+ QStringListModel model1( QStringList() << "0" << "1" << "2" << "3" << "4" << "5" );
+ QStringListModel model2( QStringList() << "a" << "b" << "c" );
+ QSortFilterProxyModel proxy;
+
+ QHeaderView view(Qt::Vertical);
+ view.setModel(&proxy);
+ view.show();
+ QTest::qWait(20);
+
+ proxy.setSourceModel(&model1);
+ QApplication::processEvents();
+ view.swapSections(0,2);
+ view.swapSections(1,4);
+ QApplication::processEvents();
+ QCOMPARE(checkHeaderViewOrder(&view, QVector<int>() << 2 << 4 << 0 << 3 << 1 << 5) , 0);
+
+ proxy.setSourceModel(&model2);
+ QApplication::processEvents();
+ QCOMPARE(checkHeaderViewOrder(&view, QVector<int>() << 2 << 0 << 1 ) , 0);
+
+ proxy.setSourceModel(&model1);
+ QApplication::processEvents();
+ QCOMPARE(checkHeaderViewOrder(&view, QVector<int>() << 2 << 0 << 1 << 3 << 4 << 5 ) , 0);
+}
+
+
QTEST_MAIN(tst_QHeaderView)
#include "tst_qheaderview.moc"