diff options
-rw-r--r-- | src/controls/ComboBox.qml | 46 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_combobox.qml | 75 |
2 files changed, 107 insertions, 14 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 diff --git a/tests/auto/controls/data/tst_combobox.qml b/tests/auto/controls/data/tst_combobox.qml index e6ea7c5c..802d23e4 100644 --- a/tests/auto/controls/data/tst_combobox.qml +++ b/tests/auto/controls/data/tst_combobox.qml @@ -118,15 +118,13 @@ TestCase { } function test_arraymodelwithtextrole() { - var arrayModel = [ - {text: 'Banana', color: 'Yellow'}, - {text: 'Apple', color: 'Green'}, - {text: 'Coconut', color: 'Brown'} - ]; - - var comboBox = Qt.createQmlObject('import QtQuick.Controls 1.1 ; ComboBox {}', testCase, ''); - comboBox.textRole = "text" - comboBox.model = arrayModel + // FIXME The use-case before this change should work. + var comboBox = Qt.createQmlObject('import QtQuick.Controls 1.1 ; \ + ComboBox { \ + model: [ { "text": "Banana", "color": "Yellow"}, \ + { "text": "Apple", "color": "Green"}, \ + { "text": "Coconut", "color": "Brown"} ]; \ + textRole: "text" }', testCase, ''); compare(comboBox.currentIndex, 0) compare(comboBox.currentText, "Banana") comboBox.textRole = "color" @@ -535,6 +533,65 @@ TestCase { comboBox.destroy() } + SignalSpy { + id: modelSpy + signalName: "modelChanged" + } + + SignalSpy { + id: textSpy + signalName: "currentTextChanged" + } + + SignalSpy { + id: indexSpy + signalName: "currentIndexChanged" + } + + function test_modelChange() { + var comboBox = Qt.createQmlObject('import QtQuick.Controls 1.1 ; ComboBox { model: ["a", "b", "c", "d"] }', container, ''); + modelSpy.target = textSpy.target = indexSpy.target = comboBox + + compare(comboBox.currentIndex, 0) + compare(comboBox.currentText, "a") + + // 1st model change + comboBox.model = ["A", "B", "C", "D"] + compare(comboBox.currentIndex, 0) + compare(modelSpy.count, 1) + compare(indexSpy.count, 0) + compare(textSpy.count, 1) + modelSpy.clear() + indexSpy.clear() + textSpy.clear() + + // Setting currentIndex + comboBox.currentIndex = 3 + compare(indexSpy.count, 1) + compare(textSpy.count, 1) + indexSpy.clear() + textSpy.clear() + + // 2nd model change + comboBox.model = 4 + compare(comboBox.currentIndex, 0) + compare(modelSpy.count, 1) + compare(indexSpy.count, 1) + compare(textSpy.count, 1) + modelSpy.clear() + indexSpy.clear() + textSpy.clear() + + // 3rd model change + comboBox.model = ["a", "b", "c", "d"] + compare(comboBox.currentIndex, 0) + compare(modelSpy.count, 1) + compare(indexSpy.count, 0) + compare(textSpy.count, 1) + + comboBox.destroy() + } + function test_addRemoveItemsInModel_QTBUG_30379() { var comboBox = Qt.createQmlObject('import QtQuick.Controls 1.1 ; ComboBox {}', testCase, ''); comboBox.textRole = "text" |