summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Mardegan <mardy@users.sourceforge.net>2020-02-29 19:08:44 +0300
committerAlberto Mardegan <mardy@users.sourceforge.net>2020-03-10 14:50:02 +0300
commitb26bfb18616e2a4baf33f9f1a2c147e3f5512de6 (patch)
tree6094e51f0a87ad52199b6ded1500f4bc3165a97b
parent59d15ec7f1dc19634f47d2aa6fa17f749fa54cd4 (diff)
downloadqtquickcontrols-b26bfb18616e2a4baf33f9f1a2c147e3f5512de6.tar.gz
TreeView: update currentIndex on model changes
The previous binding was not getting re-evaluated when the model was being reset or when rows were been inserted or removed. Here we use a counter variable, whose value change will force the evaluation of the binding. Fixes: QTBUG-53097 Change-Id: I76afebbda78ab477cf65631337a8bad51ca5428d Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--src/controls/TreeView.qml8
-rw-r--r--tests/auto/controls/data/tst_treeview.qml25
2 files changed, 32 insertions, 1 deletions
diff --git a/src/controls/TreeView.qml b/src/controls/TreeView.qml
index 6a38acff..2bedb9e6 100644
--- a/src/controls/TreeView.qml
+++ b/src/controls/TreeView.qml
@@ -49,7 +49,7 @@ BasicTableView {
property var model: null
property alias rootIndex: modelAdaptor.rootIndex
- readonly property var currentIndex: modelAdaptor.mapRowToModelIndex(__currentRow)
+ readonly property var currentIndex: modelAdaptor.updateCount, modelAdaptor.mapRowToModelIndex(__currentRow)
property ItemSelectionModel selection: null
signal activated(var index)
@@ -96,6 +96,12 @@ BasicTableView {
id: modelAdaptor
model: root.model
+ // Hack to force re-evaluation of the currentIndex binding
+ property int updateCount: 0
+ onModelReset: updateCount++
+ onRowsInserted: updateCount++
+ onRowsRemoved: updateCount++
+
onExpanded: root.expanded(index)
onCollapsed: root.collapsed(index)
}
diff --git a/tests/auto/controls/data/tst_treeview.qml b/tests/auto/controls/data/tst_treeview.qml
index 902d2178..324d9cff 100644
--- a/tests/auto/controls/data/tst_treeview.qml
+++ b/tests/auto/controls/data/tst_treeview.qml
@@ -848,5 +848,30 @@ Item {
mouseClick(tree, semiIndent + 50, 20 + 50, Qt.LeftButton)
verify(selectionModel.isSelected(parentItem))
}
+
+ function test_QTBUG_53097_currentIndex_on_model_reset()
+ {
+ var component = Qt.createComponent("treeview/treeview_1.qml")
+ compare(component.status, Component.Ready)
+ var tree = component.createObject(container);
+ verify(tree !== null, "tree created is null")
+ tree.headerVisible = false
+ var model = tree.model
+ waitForRendering(tree)
+
+ /* Select the first row */
+ verify(!tree.currentIndex.valid)
+ mouseClick(tree, semiIndent + 50, 20, Qt.LeftButton)
+ compare(tree.currentIndex.row, 0)
+
+ spy.clear()
+ spy.target = tree
+ spy.signalName = "currentIndexChanged"
+ compare(spy.count, 0)
+
+ /* delete the row: the currentIndex must be updated */
+ model.removeRows(0, 1)
+ compare(spy.count, 1)
+ }
}
}