From 08a32dff66a89fb517e86d3a2232407bd37fb715 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Wed, 8 Jan 2014 12:41:45 +0100 Subject: ComboBox: Allow setting currentIndex to -1 to clear selection Task-number: QTBUG-35794 [ChangeLog][QtQuickControls]ComboBox: Allow setting currentIndex to -1 to clear selection Change-Id: I034b061484ab095a9ec049fb3b35614b98bfb956 Reviewed-by: Jens Bache-Wiig --- src/controls/ComboBox.qml | 25 ++++++++++++++++++++++--- 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 974af0ae..74def897 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 fde8444c..6758c982 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() + } + } } } -- cgit v1.2.1