From 6420f43f30e2d5cf7ae74702f96c176d7bf22a84 Mon Sep 17 00:00:00 2001
From: Olivier Goffart <ogoffart@trolltech.com>
Date: Tue, 13 Oct 2009 17:40:20 +0200
Subject: Fix sorting after changing a QTableView's header

Keep the sorting states in sync with the header when
setting custom headers

Reviewed-by: Gabriel
Task-number: QTBUG-3128
task-number: 234926
---
 src/gui/itemviews/qtableview.cpp         |  3 +++
 tests/auto/qtableview/tst_qtableview.cpp | 37 ++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/src/gui/itemviews/qtableview.cpp b/src/gui/itemviews/qtableview.cpp
index 2d98258..a610b73 100644
--- a/src/gui/itemviews/qtableview.cpp
+++ b/src/gui/itemviews/qtableview.cpp
@@ -1117,6 +1117,9 @@ void QTableView::setHorizontalHeader(QHeaderView *header)
     connect(d->horizontalHeader, SIGNAL(sectionHandleDoubleClicked(int)),
             this, SLOT(resizeColumnToContents(int)));
     connect(d->horizontalHeader, SIGNAL(geometriesChanged()), this, SLOT(updateGeometries()));
+
+    //update the sorting enabled states on the new header
+    setSortingEnabled(d->sortingEnabled);
 }
 
 /*!
diff --git a/tests/auto/qtableview/tst_qtableview.cpp b/tests/auto/qtableview/tst_qtableview.cpp
index eab5a35..5b11823 100644
--- a/tests/auto/qtableview/tst_qtableview.cpp
+++ b/tests/auto/qtableview/tst_qtableview.cpp
@@ -193,6 +193,7 @@ private slots:
     void mouseWheel();
 
     void addColumnWhileEditing();
+    void task234926_setHeaderSorting();
 };
 
 // Testing get/set functions
@@ -3774,5 +3775,41 @@ void tst_QTableView::task191545_dragSelectRows()
     }
 }
 
+void tst_QTableView::task234926_setHeaderSorting()
+{
+    QStringListModel model;
+    QStringList data;
+    data << "orange" << "apple" << "banana" << "lemon" << "pumpkin";
+    QStringList sortedDataA = data;
+    QStringList sortedDataD = data;
+    qSort(sortedDataA);
+    qSort(sortedDataD.begin(), sortedDataD.end(), qGreater<const QString &>());
+    model.setStringList(data);
+    QTableView view;
+    view.setModel(&model);
+//    view.show();
+    QTest::qWait(20);
+    QCOMPARE(model.stringList(), data);
+    view.setSortingEnabled(true);
+    view.sortByColumn(0, Qt::AscendingOrder);
+    QApplication::processEvents();
+    QCOMPARE(model.stringList() , sortedDataA);
+
+    view.horizontalHeader()->setSortIndicator(0, Qt::DescendingOrder);
+    QApplication::processEvents();
+    QCOMPARE(model.stringList() , sortedDataD);
+
+    QHeaderView *h = new QHeaderView(Qt::Horizontal);
+    h->setModel(&model);
+    view.setHorizontalHeader(h);
+    h->setSortIndicator(0, Qt::AscendingOrder);
+    QApplication::processEvents();
+    QCOMPARE(model.stringList() , sortedDataA);
+
+    h->setSortIndicator(0, Qt::DescendingOrder);
+    QApplication::processEvents();
+    QCOMPARE(model.stringList() , sortedDataD);
+}
+
 QTEST_MAIN(tst_QTableView)
 #include "tst_qtableview.moc"
-- 
cgit v0.12