diff options
author | Gabriel de Dietrich <gabriel.dedietrich@digia.com> | 2014-01-08 12:41:45 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-01-08 16:45:28 +0100 |
commit | 08a32dff66a89fb517e86d3a2232407bd37fb715 (patch) | |
tree | 459ef04e6cbaa26dc68f0c5f795c879adbd58422 | |
parent | 7306cfd789274ef5f53e964a63d92b60352bd853 (diff) |
ComboBox: Allow setting currentIndex to -1 to clear selectionv5.2.1
Task-number: QTBUG-35794
[ChangeLog][QtQuickControls]ComboBox: Allow setting currentIndex to -1 to clear selection
Change-Id: I034b061484ab095a9ec049fb3b35614b98bfb956
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
-rw-r--r-- | src/controls/ComboBox.qml | 25 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_combobox.qml | 17 |
2 files changed, 39 insertions, 3 deletions
diff --git a/src/controls/ComboBox.qml b/src/controls/ComboBox.qml index 974af0ae5..74def8973 100644 --- a/src/controls/ComboBox.qml +++ b/src/controls/ComboBox.qml @@ -119,6 +119,9 @@ Control { /*! \qmlproperty int ComboBox::currentIndex The index of the currently selected item in the ComboBox. + Setting currentIndex to \c -1 will reset the selection and clear the text + label. If \l editable is \c true, you may also need to manually clear \l editText. + \sa model */ property alias currentIndex: popup.__selectedIndex @@ -342,7 +345,6 @@ Control { enabled: editable focus: true clip: contentWidth > width - text: currentText anchors.fill: parent anchors.leftMargin: 8 @@ -358,9 +360,11 @@ Control { onAccepted: { var idx = input.find(editText, Qt.MatchFixedString) if (idx > -1) { + editTextMatches = true; currentIndex = idx; editText = textAt(idx); } else { + editTextMatches = false; currentIndex = -1; popup.currentText = editText; } @@ -369,6 +373,7 @@ Control { property bool blockUpdate: false property string prevText + property bool editTextMatches: true function find (text, searchType) { for (var i = 0 ; i < popupItems.count ; ++i) { @@ -437,6 +442,13 @@ Control { } } + Binding { + target: input + property: "text" + value: popup.currentText + when: input.editTextMatches + } + onTextRoleChanged: popup.resolveTextValue(textRole) Menu { @@ -449,7 +461,12 @@ Control { onSelectedTextChanged: if (selectedText) popup.currentText = selectedText property string selectedText - on__SelectedIndexChanged: updateSelectedText() + on__SelectedIndexChanged: { + if (__selectedIndex === -1) + popup.currentText = "" + else + updateSelectedText() + } property string textRole: "" property bool ready: false @@ -559,8 +576,10 @@ Control { function updateSelectedText() { var selectedItem; - if (__selectedIndex !== -1 && (selectedItem = items[__selectedIndex])) + if (__selectedIndex !== -1 && (selectedItem = items[__selectedIndex])) { + input.editTextMatches = true selectedText = selectedItem.text + } } } diff --git a/tests/auto/controls/data/tst_combobox.qml b/tests/auto/controls/data/tst_combobox.qml index fde8444cb..6758c9825 100644 --- a/tests/auto/controls/data/tst_combobox.qml +++ b/tests/auto/controls/data/tst_combobox.qml @@ -680,5 +680,22 @@ TestCase { } return index } + + function test_minusOneIndexResetsSelection_QTBUG_35794() { + var qmlObjects = ['import QtQuick.Controls 1.1 ; ComboBox { model: ["A", "B", "C"] }', + 'import QtQuick.Controls 1.1 ; ComboBox { editable: true; model: ["A", "B", "C"] }'] + for (var i = 0; i < qmlObjects.length; i++) { + var comboBox = Qt.createQmlObject(qmlObjects[i], testCase, ''); + compare(comboBox.currentIndex, 0) + compare(comboBox.currentText, "A") + comboBox.currentIndex = -1 + compare(comboBox.currentIndex, -1) + compare(comboBox.currentText, "") + comboBox.currentIndex = 1 + compare(comboBox.currentIndex, 1) + compare(comboBox.currentText, "B") + comboBox.destroy() + } + } } } |