Commit 188c2ef11e92d04dcf334309c85a7f1b14945aaa

QHeaderView: fixes sectionClicked() emitted with wrong section index

The obvious fix is to use the previsously computed 'section' as paramatter.
(It is even faster)

But one might wonder why logicalIndexAt() does not return the same result
before and after flipSortIndicator().  The reason is that while being
sorted, in _q_layoutChanged, all the hidden section where unhidden and hidden
again. Leaving some pending computation.

Task-number: QTBUG-7833
Reviewed-by: Gabriel
  
18531853 persistentHiddenSections.clear();
18541854 return;
18551855 }
1856
1857 QBitArray oldSectionHidden = sectionHidden;
18561858 bool sectionCountChanged = false;
1857 for (int i = 0; i < sectionHidden.count(); ++i) {
1858 if (sectionHidden.testBit(i))
1859 q->setSectionHidden(logicalIndex(i), false);
1860 }
18611859
18621860 for (int i = 0; i < persistentHiddenSections.count(); ++i) {
18631861 QModelIndex index = persistentHiddenSections.at(i);
18641864 ? index.column()
18651865 : index.row());
18661866 q->setSectionHidden(logical, true);
1867 oldSectionHidden.setBit(logical, false);
18671868 } else if (!sectionCountChanged && (modelSectionCount() != sectionCount)) {
18681869 sectionCountChanged = true;
18691870 break;
18721872 }
18731873 persistentHiddenSections.clear();
18741874
1875 for (int i = 0; i < oldSectionHidden.count(); ++i) {
1876 if (oldSectionHidden.testBit(i))
1877 q->setSectionHidden(logicalIndex(i), false);
1878 }
1879
18751880 // the number of sections changed; we need to reread the state of the model
18761881 if (sectionCountChanged)
18771882 q->initializeSections();
23042304 int section = logicalIndexAt(pos);
23052305 if (section != -1 && section == d->pressed) {
23062306 d->flipSortIndicator(section);
2307 emit sectionClicked(logicalIndexAt(pos));
2307 emit sectionClicked(section);
23082308 }
23092309 if (d->pressed != -1)
23102310 updateSection(d->pressed);
  
4444#include <QStandardItemModel>
4545#include <QStringListModel>
4646#include <QSortFilterProxyModel>
47#include <QTableView>
4748
4849#include <qabstractitemmodel.h>
4950#include <qapplication.h>
191191 void task236450_hidden();
192192 void task248050_hideRow();
193193 void QTBUG6058_reset();
194 void QTBUG7833_sectionClicked();
194195
195196protected:
196197 QHeaderView *view;
19921992 proxy.setSourceModel(&model1);
19931993 QApplication::processEvents();
19941994 QCOMPARE(checkHeaderViewOrder(&view, QVector<int>() << 2 << 0 << 1 << 3 << 4 << 5 ) , 0);
1995}
1996
1997void tst_QHeaderView::QTBUG7833_sectionClicked()
1998{
1999
2000
2001
2002
2003 QTableView tv;
2004 QStandardItemModel *sim = new QStandardItemModel(&tv);
2005 QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(&tv);
2006 proxyModel->setSourceModel(sim);
2007 proxyModel->setDynamicSortFilter(true);
2008 proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
2009
2010 QList<QStandardItem *> row;
2011 for (int i = 0; i < 12; i++)
2012 row.append(new QStandardItem(QString(QLatin1Char('A' + i))));
2013 sim->appendRow(row);
2014 row.clear();
2015 for (int i = 12; i > 0; i--)
2016 row.append(new QStandardItem(QString(QLatin1Char('A' + i))));
2017 sim->appendRow(row);
2018
2019 tv.setSortingEnabled(true);
2020 tv.horizontalHeader()->setSortIndicatorShown(true);
2021 tv.horizontalHeader()->setClickable(true);
2022 tv.horizontalHeader()->setStretchLastSection(true);
2023 tv.horizontalHeader()->setResizeMode(QHeaderView::Interactive);
2024
2025 tv.setModel(proxyModel);
2026 tv.setColumnHidden(5, true);
2027 tv.setColumnHidden(6, true);
2028 tv.horizontalHeader()->swapSections(8, 10);
2029 tv.sortByColumn(1, Qt::AscendingOrder);
2030
2031 QSignalSpy clickedSpy(tv.horizontalHeader(), SIGNAL(sectionClicked(int)));
2032 QSignalSpy pressedSpy(tv.horizontalHeader(), SIGNAL(sectionPressed(int)));
2033
2034
2035 QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier,
2036 QPoint(tv.horizontalHeader()->sectionViewportPosition(11) + 5, 5));
2037 QCOMPARE(clickedSpy.count(), 1);
2038 QCOMPARE(pressedSpy.count(), 1);
2039 QCOMPARE(clickedSpy.at(0).at(0).toInt(), 11);
2040 QCOMPARE(pressedSpy.at(0).at(0).toInt(), 11);
2041
2042 QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier,
2043 QPoint(tv.horizontalHeader()->sectionViewportPosition(8) + 5, 5));
2044
2045 QCOMPARE(clickedSpy.count(), 2);
2046 QCOMPARE(pressedSpy.count(), 2);
2047 QCOMPARE(clickedSpy.at(1).at(0).toInt(), 8);
2048 QCOMPARE(pressedSpy.at(1).at(0).toInt(), 8);
2049
2050 QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier,
2051 QPoint(tv.horizontalHeader()->sectionViewportPosition(0) + 5, 5));
2052
2053 QCOMPARE(clickedSpy.count(), 3);
2054 QCOMPARE(pressedSpy.count(), 3);
2055 QCOMPARE(clickedSpy.at(2).at(0).toInt(), 0);
2056 QCOMPARE(pressedSpy.at(2).at(0).toInt(), 0);
19952057}
19962058
19972059