aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2020-05-11 15:45:31 +0200
committerQt Cherry-pick Bot <cherrypickbot@codereview.qt-project.org>2020-05-12 10:49:28 +0000
commitbf9397c535971ec38668200ab465ebf20fa55ccf (patch)
treef3e4a9f2c57fb0f22a3a2f1bc3a55808d1d40533
parent80a1ea20d66dea11c74d0a40e1c75b8a782f38a1 (diff)
ComboBox: fix currentValue not being updated on model changes
Make sure we call updateCurrentValue() where necessary. Fixes: QTBUG-83554 Change-Id: Iad593c2fc094a26429de1eda91bbdb152ffee2c2 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io> (cherry picked from commit 3adeceaa1cfe5fae55ca6bd75226c52363a7e7d3) Reviewed-by: Qt Cherry-pick Bot
-rw-r--r--src/quicktemplates2/qquickcombobox.cpp12
-rw-r--r--tests/auto/controls/data/tst_combobox.qml49
2 files changed, 55 insertions, 6 deletions
diff --git a/src/quicktemplates2/qquickcombobox.cpp b/src/quicktemplates2/qquickcombobox.cpp
index d9bbd2bd..d7a67325 100644
--- a/src/quicktemplates2/qquickcombobox.cpp
+++ b/src/quicktemplates2/qquickcombobox.cpp
@@ -404,13 +404,13 @@ void QQuickComboBoxPrivate::createdItem(int index, QObject *object)
}
if (index == currentIndex && !q->isEditable())
- updateCurrentText();
+ updateCurrentTextAndValue();
}
void QQuickComboBoxPrivate::modelUpdated()
{
if (!extra.isAllocated() || !extra->accepting)
- updateCurrentText();
+ updateCurrentTextAndValue();
}
void QQuickComboBoxPrivate::countChanged()
@@ -866,11 +866,11 @@ void QQuickComboBox::setModel(const QVariant& m)
if (QAbstractItemModel* aim = qvariant_cast<QAbstractItemModel *>(d->model)) {
QObjectPrivate::disconnect(aim, &QAbstractItemModel::dataChanged,
- d, QOverload<>::of(&QQuickComboBoxPrivate::updateCurrentText));
+ d, QOverload<>::of(&QQuickComboBoxPrivate::updateCurrentTextAndValue));
}
if (QAbstractItemModel* aim = qvariant_cast<QAbstractItemModel *>(model)) {
QObjectPrivate::connect(aim, &QAbstractItemModel::dataChanged,
- d, QOverload<>::of(&QQuickComboBoxPrivate::updateCurrentText));
+ d, QOverload<>::of(&QQuickComboBoxPrivate::updateCurrentTextAndValue));
}
d->model = model;
@@ -878,7 +878,7 @@ void QQuickComboBox::setModel(const QVariant& m)
emit countChanged();
if (isComponentComplete()) {
setCurrentIndex(count() > 0 ? 0 : -1);
- d->updateCurrentText();
+ d->updateCurrentTextAndValue();
}
emit modelChanged();
}
@@ -1869,7 +1869,7 @@ bool QQuickComboBox::event(QEvent *e)
{
Q_D(QQuickComboBox);
if (e->type() == QEvent::LanguageChange)
- d->updateCurrentText();
+ d->updateCurrentTextAndValue();
return QQuickControl::event(e);
}
diff --git a/tests/auto/controls/data/tst_combobox.qml b/tests/auto/controls/data/tst_combobox.qml
index 2d5069b3..e9156fee 100644
--- a/tests/auto/controls/data/tst_combobox.qml
+++ b/tests/auto/controls/data/tst_combobox.qml
@@ -296,12 +296,30 @@ TestCase {
ListElement { name: "Banana"; color: "yellow" }
}
+ Component {
+ id: fruitModelComponent
+ ListModel {
+ ListElement { name: "Apple"; color: "red" }
+ ListElement { name: "Orange"; color: "orange" }
+ ListElement { name: "Banana"; color: "yellow" }
+ }
+ }
+
property var fruitarray: [
{ name: "Apple", color: "red" },
{ name: "Orange", color: "orange" },
{ name: "Banana", color: "yellow" }
]
+ Component {
+ id: birdModelComponent
+ ListModel {
+ ListElement { name: "Galah"; color: "pink" }
+ ListElement { name: "Kookaburra"; color: "brown" }
+ ListElement { name: "Magpie"; color: "black" }
+ }
+ }
+
function test_textRole_data() {
return [
{ tag: "ListModel", model: fruitmodel },
@@ -447,6 +465,37 @@ TestCase {
compare(control.indexOfValue(data.tag), data.expectedIndex)
}
+ function test_currentValueAfterModelChanged() {
+ let fruitModel = createTemporaryObject(fruitModelComponent, testCase)
+ verify(fruitModel)
+
+ let control = createTemporaryObject(comboBox, testCase,
+ { model: fruitModel, textRole: "name", valueRole: "color", currentIndex: 1 })
+ verify(control)
+ compare(control.currentText, "Orange")
+ compare(control.currentValue, "orange")
+
+ // Remove "Apple"; the current item should now be "Banana", so currentValue should be "yellow".
+ fruitModel.remove(0)
+ compare(control.currentText, "Banana")
+ compare(control.currentValue, "yellow")
+ }
+
+ function test_currentValueAfterNewModelSet() {
+ let control = createTemporaryObject(comboBox, testCase,
+ { model: fruitmodel, textRole: "name", valueRole: "color", currentIndex: 0 })
+ verify(control)
+ compare(control.currentText, "Apple")
+ compare(control.currentValue, "red")
+
+ // Swap the model out entirely. Since the currentIndex was 0 and
+ // is reset to 0 when a new model is set, it remains 0.
+ let birdModel = createTemporaryObject(birdModelComponent, testCase)
+ verify(birdModel)
+ control.model = birdModel
+ compare(control.currentText, "Galah")
+ compare(control.currentValue, "pink")
+ }
function test_arrowKeys() {
var control = createTemporaryObject(comboBox, testCase,