aboutsummaryrefslogtreecommitdiffstats
path: root/src/imports/templates/qtquicktemplates2plugin.cpp
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2019-03-04 11:12:05 +0100
committerMitch Curtis <mitch.curtis@qt.io>2019-04-23 11:11:18 +0000
commitf462312365d4955fc82b247b72f84e1c77d8104d (patch)
tree0b240fda0fe60d5110b91aecc6da413f5b086b81 /src/imports/templates/qtquicktemplates2plugin.cpp
parent8b61881469901a793e9ac8eec7caefad547258a2 (diff)
Add valueRole API to ComboBox
This allows the user to conveniently manage data for a role associated with the text role. A common example of this is an enum stored in a backend with nicely formatted text displayed to the user. Before this patch, developers would have to write code like this: ComboBox { textRole: "text" onActivated: backend.modifier = model[currentIndex].value Component.onCompleted: currentIndex = findValue(backend.modifier) model: [ { value: Qt.NoModifier, text: qsTr("No modifier") }, { value: Qt.ShiftModifier, text: qsTr("Shift") }, { value: Qt.ControlModifier, text: qsTr("Control") } ] function findValue(value) { for (var i = 0; i < model.length; ++i) { if (model[i].value === value) return i } return -1 } } With this patch, the code becomes much simpler: ComboBox { textRole: "text" valueRole: "value" onActivated: backend.modifier = currentValue Component.onCompleted: currentIndex = indexOfValue(backend.modifier) model: [ { value: Qt.NoModifier, text: qsTr("No modifier") }, { value: Qt.ShiftModifier, text: qsTr("Shift") }, { value: Qt.ControlModifier, text: qsTr("Control") } ] } [ChangeLog][Controls][ComboBox] Added valueRole, currentValue and indexOfValue(). These allow convenient management of data for a role associated with the text role. Change-Id: I0ed19bd0ba9cf6b044a8113ff1a8782d43065449 Fixes: QTBUG-73491 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/imports/templates/qtquicktemplates2plugin.cpp')
-rw-r--r--src/imports/templates/qtquicktemplates2plugin.cpp3
1 files changed, 3 insertions, 0 deletions
diff --git a/src/imports/templates/qtquicktemplates2plugin.cpp b/src/imports/templates/qtquicktemplates2plugin.cpp
index 10f9b8dd..008293a2 100644
--- a/src/imports/templates/qtquicktemplates2plugin.cpp
+++ b/src/imports/templates/qtquicktemplates2plugin.cpp
@@ -355,6 +355,9 @@ void QtQuickTemplates2Plugin::registerTypes(const char *uri)
qmlRegisterUncreatableType<QQuickSplitHandleAttached>(uri, 2, 13, "SplitHandle",
QStringLiteral("SplitHandle is only available as an attached property."));
qmlRegisterType<QQuickSplitHandleAttached>();
+
+ // QtQuick.Templates 2.14 (new types and revisions in Qt 5.14)
+ qmlRegisterType<QQuickComboBox, 14>(uri, 2, 14, "ComboBox");
}
QT_END_NAMESPACE