From 2f77a972eafacebcc5a44204dbf577258de1c4a1 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Thu, 24 Oct 2013 18:00:22 +0200 Subject: ComboBox: Minimize currentText changes when changing model Bonus change, changing the model after its initial value will reset currentIndex to 0. test_arraymodelwithtextrole needs some rework. The issue relies on textRole and model being set separately, and a late change to __modelIsArray updating the currentText when it probably shouldn't. [ChangeLog][QtQuick Controls][ComboBox] Changing the model after initialization will reset currentIndex to 0 Change-Id: If6c0dee9e022036ef888f2aef87a5c405868684b Reviewed-by: J-P Nurmi Reviewed-by: Jens Bache-Wiig --- src/controls/ComboBox.qml | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'src/controls/ComboBox.qml') diff --git a/src/controls/ComboBox.qml b/src/controls/ComboBox.qml index 4ada75e7..4065724c 100644 --- a/src/controls/ComboBox.qml +++ b/src/controls/ComboBox.qml @@ -107,14 +107,20 @@ Control { id: comboBox /*! \qmlproperty model ComboBox::model - The model to populate the ComboBox from. */ + The model to populate the ComboBox from. + + Changing the model after initialization will reset \l currentIndex to \c 0. + */ property alias model: popupItems.model /*! The model role used for populating the ComboBox. */ property string textRole: "" /*! \qmlproperty int ComboBox::currentIndex - The index of the currently selected item in the ComboBox. */ + The index of the currently selected item in the ComboBox. + + \sa model + */ property alias currentIndex: popup.__selectedIndex /*! \qmlproperty string ComboBox::currentText @@ -412,7 +418,8 @@ Control { property string currentText: selectedText onSelectedTextChanged: if (selectedText) popup.currentText = selectedText - readonly property string selectedText: items[__selectedIndex] ? items[__selectedIndex].text : "" + property string selectedText + on__SelectedIndexChanged: updateSelectedText() property string textRole: "" property bool ready: false @@ -429,6 +436,20 @@ Control { Instantiator { id: popupItems active: false + + property bool updatingModel: false + onModelChanged: { + if (active) { + if (updatingModel && popup.__selectedIndex === 0) { + // We still want to update the currentText + popup.updateSelectedText() + } else { + updatingModel = true + popup.__selectedIndex = 0 + } + } + } + MenuItem { text: popup.textRole === '' ? modelData : @@ -441,7 +462,11 @@ Control { checkable: true exclusiveGroup: eg } - onObjectAdded: popup.insertItem(index, object) + onObjectAdded: { + popup.insertItem(index, object) + if (!updatingModel && index === popup.__selectedIndex) + popup.selectedText = object["text"] + } onObjectRemoved: popup.removeItem(object) } @@ -483,8 +508,13 @@ Control { textRole = roleName } } - popupItems.active = true + + if (!popupItems.active) + popupItems.active = true + else + updateSelectedText() } + function show() { if (items[__selectedIndex]) items[__selectedIndex].checked = true @@ -494,6 +524,12 @@ Control { else __popup(0, y, isPopup ? __selectedIndex : 0) } + + function updateSelectedText() { + var selectedItem; + if (__selectedIndex !== -1 && (selectedItem = items[__selectedIndex])) + selectedText = selectedItem.text + } } // The key bindings below will only be in use when popup is -- cgit v1.2.1