From 2c916d47ef1338403cf14b4a2684159d865aa4fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Mon, 26 Aug 2013 20:26:07 +0200 Subject: [PATCH] QTreeView - fix next focus when there is no current index Before we went through non hidden indexes, but we never considered the visual order. This patch fixes that issue. Though it was wrong before, it probably never was a big problem since it was unlikely that the tree (on logical index 0) was swapped or hidden, but 658e42e77a00596b63823482c9b77644556b647c makes it more likely that problems with wrong focus could occur. Change-Id: Ic7b6cd2df1f8638be1a7c9e6df27f428685869fc Reviewed-by: Stephen Kelly --- src/widgets/itemviews/qtreeview.cpp | 4 ++-- .../widgets/itemviews/qtreeview/tst_qtreeview.cpp | 15 +++++++++++++++ .../manual/widgets/itemviews/qtreewidget/main.cpp | 3 ++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp index 54b663274d..e4c2cfcb0c 100644 --- a/src/widgets/itemviews/qtreeview.cpp +++ b/src/widgets/itemviews/qtreeview.cpp @@ -2179,10 +2179,10 @@ QModelIndex QTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifie if (!current.isValid()) { int i = d->below(-1); int c = 0; - while (c < d->header->count() && d->header->isSectionHidden(c)) + while (c < d->header->count() && d->header->isSectionHidden(d->header->logicalIndex(c))) ++c; if (i < d->viewItems.count() && c < d->header->count()) { - return d->modelIndex(i, c); + return d->modelIndex(i, d->header->logicalIndex(c)); } return QModelIndex(); } diff --git a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp index dcda9f7fd7..8d31fcdf13 100644 --- a/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp +++ b/tests/auto/widgets/itemviews/qtreeview/tst_qtreeview.cpp @@ -259,6 +259,7 @@ private slots: void taskQTBUG_25333_adjustViewOptionsForIndex(); void taskQTBUG_18539_emitLayoutChanged(); void taskQTBUG_8176_emitOnExpandAll(); + void testInitialFocus(); }; class QtTestModel: public QAbstractItemModel @@ -4239,6 +4240,20 @@ void tst_QTreeView::taskQTBUG_8176_emitOnExpandAll() QCOMPARE(spy2.size(), 1); // item2 is collapsed } +void tst_QTreeView::testInitialFocus() +{ + QTreeWidget treeWidget; + treeWidget.setColumnCount(5); + new QTreeWidgetItem(&treeWidget, QStringList(QString("1;2;3;4;5").split(";"))); + treeWidget.setTreePosition(2); + treeWidget.header()->hideSection(0); // make sure we skip hidden section(s) + treeWidget.header()->swapSections(1, 2); // make sure that we look for first visual index (and not first logical) + treeWidget.show(); + QTest::qWaitForWindowExposed(&treeWidget); + QApplication::processEvents(); + QCOMPARE(treeWidget.currentIndex().column(), 2); +} + #ifndef QT_NO_ANIMATION void tst_QTreeView::quickExpandCollapse() { diff --git a/tests/manual/widgets/itemviews/qtreewidget/main.cpp b/tests/manual/widgets/itemviews/qtreewidget/main.cpp index 9428113250..0dec329cd8 100644 --- a/tests/manual/widgets/itemviews/qtreewidget/main.cpp +++ b/tests/manual/widgets/itemviews/qtreewidget/main.cpp @@ -115,13 +115,14 @@ public: // Developer no. could also have been social security number og some other id. itemInfo.append("Title"); treeWidget->setHeaderLabels(itemInfo); - radioFirstName->setChecked(true); + radioLastName->setChecked(true); connect(radioFirstName, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool))); connect(radioLastName, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool))); connect(radioDeveloperNo, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool))); connect(radioTitle, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool))); treeWidget->setTreePosition(-1); + treeWidget->header()->swapSections(0, 1); } protected slots: