From 4624ec51b2f1672b109dfec536230f3920bdbd36 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Mon, 11 Jun 2018 13:16:12 +0200 Subject: Fix Tumbler not respecting currentIndex changes in onModelChanged The use case in the referenced bug report looks something like this: Tumbler { model: 4 // ... onModelChanged: { currentIndex = model - 2; } } The problem was that setting currentIndex in onModelChanged would cause the wrap to change to true, which in turn caused the internal view to change to PathView. This would cause the currentIndex to be set to 0 on successive model changes (i.e ++model). By keeping track of whether or not the user set the currentIndex during a model change, we can ignore changes in the internal view's currentIndex and restore the user's currentIndex afterwards. Task-number: QTBUG-68737 Change-Id: I25738f36cf58a331d1b8e50b5029b4aa1dd27db5 Reviewed-by: Richard Moe Gustavsen --- tests/auto/controls/data/tst_tumbler.qml | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'tests') diff --git a/tests/auto/controls/data/tst_tumbler.qml b/tests/auto/controls/data/tst_tumbler.qml index 058cbeca..49fd78cc 100644 --- a/tests/auto/controls/data/tst_tumbler.qml +++ b/tests/auto/controls/data/tst_tumbler.qml @@ -1112,4 +1112,70 @@ TestCase { var label = row.label; compare(label.text, "2"); } + + Component { + id: setCurrentIndexOnImperativeModelChangeComponent + + Tumbler { + onModelChanged: currentIndex = model - 2 + } + } + + function test_setCurrentIndexOnImperativeModelChange() { + var tumbler = createTemporaryObject(setCurrentIndexOnImperativeModelChangeComponent, testCase); + verify(tumbler); + + tumbler.model = 4 + compare(tumbler.count, 4); + tumblerView = findView(tumbler); + tryCompare(tumblerView, "count", 4); + + // 4 - 2 = 2 + compare(tumbler.currentIndex, 2); + + ++tumbler.model; + compare(tumbler.count, 5); + compare(tumbler.wrap, true); + tumblerView = findView(tumbler); + tryCompare(tumblerView, "count", 5); + // 5 - 2 = 3 + compare(tumbler.currentIndex, 3); + } + + Component { + id: setCurrentIndexOnDeclarativeModelChangeComponent + + Item { + property alias tumbler: tumbler + + property int setting: 4 + + Tumbler { + id: tumbler + model: setting + onModelChanged: currentIndex = model - 2 + } + } + } + + function test_setCurrentIndexOnDeclarativeModelChange() { + var root = createTemporaryObject(setCurrentIndexOnDeclarativeModelChangeComponent, testCase); + verify(root); + + var tumbler = root.tumbler; + compare(tumbler.count, 4); + compare(tumbler.wrap, false); + tumblerView = findView(tumbler); + tryCompare(tumblerView, "count", 4); + // 4 - 2 = 2 + compare(tumbler.currentIndex, 2); + + ++root.setting; + compare(tumbler.count, 5); + compare(tumbler.wrap, true); + tumblerView = findView(tumbler); + tryCompare(tumblerView, "count", 5); + // 5 - 2 = 3 + compare(tumbler.currentIndex, 3); + } } -- cgit v1.2.3