From 0d56fd5687b3b6bb747adb919c7d658c6ca1dd27 Mon Sep 17 00:00:00 2001 From: Qiang Li Date: Fri, 18 Jun 2021 09:47:39 +0800 Subject: [PATCH] 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 Reviewed-by: Volker Hilsheimer (cherry picked from commit 5d4b91ea66666f3c8d3c8640b87a33c13dbd0c2c) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qtabbar.cpp | 5 +++-- tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/widgets/widgets/qtabbar.cpp b/src/widgets/widgets/qtabbar.cpp index 99b27b0217b..11fa33ca15c 100644 --- a/src/widgets/widgets/qtabbar.cpp +++ b/src/widgets/widgets/qtabbar.cpp @@ -1435,9 +1435,10 @@ void QTabBar::setCurrentIndex(int index) d->currentIndex = index; update(); d->makeVisible(index); - tab->lastTab = oldIndex; - if (oldIndex >= 0 && oldIndex < count()) + if (d->validIndex(oldIndex)) { + tab->lastTab = oldIndex; d->layoutTab(oldIndex); + } d->layoutTab(index); #ifndef QT_NO_ACCESSIBILITY if (QAccessible::isActive()) { diff --git a/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp b/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp index 6938a83442f..1fe6e7d80e9 100644 --- a/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp +++ b/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp @@ -602,6 +602,8 @@ void tst_QTabBar::selectionBehaviorOnRemove_data() // every other one 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; }