From 9310d5fb5285ae5eb4b94c01606791319b775b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= Date: Mon, 6 May 2019 14:09:11 +0200 Subject: Container: Keep currentIndex at 0 when removing item 0 if possible Current behavior is to always decrement the currentIndex when the current item is removed -- even when the current item is item 0. This means, for example, that in a TabBar with three tabs and the first tab selected closing the first tab will leave nothing at all selected. Change behavior to keep currentIndex at 0 if there are still items left in the container. Now closing the first tab will leave the next remaining tab selected. Change-Id: If4e1903366e29fcee8226b776d5b2e03cec189df Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquickcontainer.cpp | 4 ++-- tests/auto/controls/data/tst_tabbar.qml | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/quicktemplates2/qquickcontainer.cpp b/src/quicktemplates2/qquickcontainer.cpp index 2c357f82..8314230c 100644 --- a/src/quicktemplates2/qquickcontainer.cpp +++ b/src/quicktemplates2/qquickcontainer.cpp @@ -294,8 +294,9 @@ void QQuickContainerPrivate::removeItem(int index, QQuickItem *item) updatingCurrent = true; + int count = contentModel->count(); bool currentChanged = false; - if (index == currentIndex) { + if (index == currentIndex && (index != 0 || count == 1)) { q->setCurrentIndex(currentIndex - 1); } else if (index < currentIndex) { --currentIndex; @@ -308,7 +309,6 @@ void QQuickContainerPrivate::removeItem(int index, QQuickItem *item) q->itemRemoved(index, item); - int count = contentModel->count(); for (int i = index; i < count; ++i) q->itemMoved(i, itemAt(i)); diff --git a/tests/auto/controls/data/tst_tabbar.qml b/tests/auto/controls/data/tst_tabbar.qml index 519b1d53..42e767f2 100644 --- a/tests/auto/controls/data/tst_tabbar.qml +++ b/tests/auto/controls/data/tst_tabbar.qml @@ -270,6 +270,29 @@ TestCase { compare(contentChildrenSpy.count, 12) } + function test_removeCurrent() { + var control = createTemporaryObject(tabBar, testCase) + + control.addItem(tabButton.createObject(control, {text: "1"})) + control.addItem(tabButton.createObject(control, {text: "2"})) + control.addItem(tabButton.createObject(control, {text: "3"})) + control.currentIndex = 1 + compare(control.count, 3) + compare(control.currentIndex, 1) + + control.removeItem(1) + compare(control.count, 2) + compare(control.currentIndex, 0) + + control.removeItem(0) + compare(control.count, 1) + compare(control.currentIndex, 0) + + control.removeItem(0) + compare(control.count, 0) + compare(control.currentIndex, -1) + } + Component { id: contentBar TabBar { -- cgit v1.2.3