summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJüri Valdmann <juri.valdmann@qt.io>2019-05-06 14:09:11 +0200
committerJüri Valdmann <juri.valdmann@qt.io>2019-05-07 11:17:58 +0000
commit9310d5fb5285ae5eb4b94c01606791319b775b85 (patch)
tree5f951759082b5eca0781c0cddef812271614da4a
parent7d001497ddcda501c55aac35b4c7a60980d9c6b5 (diff)
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 <mitch.curtis@qt.io>
-rw-r--r--src/quicktemplates2/qquickcontainer.cpp4
-rw-r--r--tests/auto/controls/data/tst_tabbar.qml23
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 {