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: