diff options
author | Gabriel de Dietrich <gabriel.dedietrich@digia.com> | 2013-10-24 18:00:22 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-10-25 12:58:15 +0200 |
commit | 2f77a972eafacebcc5a44204dbf577258de1c4a1 (patch) | |
tree | 3e04628adf8dd7cada5098bc9ae300c79f11f3f7 /src/controls/ComboBox.qml | |
parent | 29d771e2ac808750a6d8fa2225a848115af11d34 (diff) | |
download | qtquickcontrols-2f77a972eafacebcc5a44204dbf577258de1c4a1.tar.gz |
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 <jpnurmi@digia.com>
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
Diffstat (limited to 'src/controls/ComboBox.qml')
-rw-r--r-- | src/controls/ComboBox.qml | 46 |
1 files changed, 41 insertions, 5 deletions
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 |