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

658e42e77a

makes it more likely that problems with wrong focus could occur.

Change-Id: Ic7b6cd2df1f8638be1a7c9e6df27f428685869fc
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Thorbjørn Martsum 2013-08-26 20:26:07 +02:00 committed by The Qt Project
parent 427037c75d
commit 2c916d47ef
3 changed files with 19 additions and 3 deletions

View File

@ -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();
}

View File

@ -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()
{

View File

@ -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: