Fix QTabBar's SelectPreviousTab behavior

When setCurrentIndex is called by removeTab, the old current index
might no longer be valid. Only update the lastTab value of the new
current tab if the old current index is still valid.

As a drive-by, use the validIndex helper function.

Fixes: QTBUG-94352
Change-Id: I945e2093a90a1fccbba86d32b1113f83fedd41de
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 5d4b91ea66666f3c8d3c8640b87a33c13dbd0c2c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Qiang Li 2021-06-18 09:47:39 +08:00 committed by Qt Cherry-pick Bot
parent 0535f79f03
commit 0d56fd5687
2 changed files with 5 additions and 2 deletions

View File

@ -1435,9 +1435,10 @@ void QTabBar::setCurrentIndex(int index)
d->currentIndex = index; d->currentIndex = index;
update(); update();
d->makeVisible(index); d->makeVisible(index);
tab->lastTab = oldIndex; if (d->validIndex(oldIndex)) {
if (oldIndex >= 0 && oldIndex < count()) tab->lastTab = oldIndex;
d->layoutTab(oldIndex); d->layoutTab(oldIndex);
}
d->layoutTab(index); d->layoutTab(index);
#ifndef QT_NO_ACCESSIBILITY #ifndef QT_NO_ACCESSIBILITY
if (QAccessible::isActive()) { if (QAccessible::isActive()) {

View File

@ -602,6 +602,8 @@ void tst_QTabBar::selectionBehaviorOnRemove_data()
// every other one // every other one
QTest::newRow("previous-10") << QTabBar::SelectPreviousTab << 7 << (IntList() << 0 << 2 << 4 << 6) << (IntList() << 6 << 4) << 2; QTest::newRow("previous-10") << QTabBar::SelectPreviousTab << 7 << (IntList() << 0 << 2 << 4 << 6) << (IntList() << 6 << 4) << 2;
// QTBUG-94352
QTest::newRow("QTBUG-94352") << QTabBar::SelectPreviousTab << 4 << (IntList() << 3) << (IntList() << 2 << 2) << 0;
} }