diff --git a/src/controls/ComboBox.qml b/src/controls/ComboBox.qml index 3f2798e06aabf448127a0885d3460ce1cbc32268..f302aef84a993be014f5e8081a13196617913b12 100644 --- a/src/controls/ComboBox.qml +++ b/src/controls/ComboBox.qml @@ -143,12 +143,16 @@ Control { property ExclusiveGroup eg: ExclusiveGroup { id: eg } + property bool __modelIsArray: popupItems.model ? popupItems.model.constructor === Array : false + Instantiator { id: popupItems active: popup.ready MenuItem { - text: popup.textRole === "" ? modelData : - (model[popup.textRole] || "") + text: popup.textRole === '' ? + modelData : + ((popup.__modelIsArray ? modelData[popup.textRole] : model[popup.textRole]) || '') + checkable: true exclusiveGroup: eg } @@ -160,12 +164,17 @@ Control { if (!ready || !model) return; - var modelMayHaveRoles = model["get"] !== undefined + var get = model['get']; + if (!get && popup.__modelIsArray) { + get = function(i) { return model[i]; } + } + + var modelMayHaveRoles = get !== undefined textRole = initialTextRole - if (textRole === "" && modelMayHaveRoles && model.get(0)) { + if (textRole === "" && modelMayHaveRoles && get(0)) { // No text role set, check whether model has a suitable role // If 'text' is found, or there's only one role, pick that. - var listElement = model.get(0) + var listElement = get(0) var roleName = "" var roleCount = 0 for (var role in listElement) { diff --git a/tests/auto/controls/data/tst_combobox.qml b/tests/auto/controls/data/tst_combobox.qml index 3a0f05688014cb536ed570b5d38bc549e4388304..2cd60278acb35d7c99746facbd424497b8cc4ead 100644 --- a/tests/auto/controls/data/tst_combobox.qml +++ b/tests/auto/controls/data/tst_combobox.qml @@ -80,4 +80,33 @@ TestCase { comboBox.textRole = "color" compare(comboBox.currentText, "Yellow") } + + function test_arraymodel() { + var arrayModel = [ + 'Banana', + 'Apple', + 'Coconut' + ]; + + var comboBox = Qt.createQmlObject('import QtQuick.Controls 1.0 ; ComboBox {}', testCase, ''); + comboBox.model = arrayModel + compare(comboBox.currentIndex, 0) + compare(comboBox.currentText, "Banana") + } + + 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.0 ; ComboBox {}', testCase, ''); + comboBox.textRole = "text" + comboBox.model = arrayModel + compare(comboBox.currentIndex, 0) + compare(comboBox.currentText, "Banana") + comboBox.textRole = "color" + compare(comboBox.currentText, "Yellow") + } }