diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2019-03-04 11:12:05 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2019-04-23 11:11:18 +0000 |
commit | f462312365d4955fc82b247b72f84e1c77d8104d (patch) | |
tree | 0b240fda0fe60d5110b91aecc6da413f5b086b81 /src/quicktemplates2/qquickcombobox_p.h | |
parent | 8b61881469901a793e9ac8eec7caefad547258a2 (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/quicktemplates2/qquickcombobox_p.h')
-rw-r--r-- | src/quicktemplates2/qquickcombobox_p.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/quicktemplates2/qquickcombobox_p.h b/src/quicktemplates2/qquickcombobox_p.h index 75e535a9..a55541d4 100644 --- a/src/quicktemplates2/qquickcombobox_p.h +++ b/src/quicktemplates2/qquickcombobox_p.h @@ -86,6 +86,9 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickComboBox : public QQuickControl Q_PROPERTY(qreal implicitIndicatorWidth READ implicitIndicatorWidth NOTIFY implicitIndicatorWidthChanged FINAL REVISION 5) Q_PROPERTY(qreal implicitIndicatorHeight READ implicitIndicatorHeight NOTIFY implicitIndicatorHeightChanged FINAL REVISION 5) Q_CLASSINFO("DeferredPropertyNames", "background,contentItem,indicator,popup") + // 2.14 (Qt 5.14) + Q_PROPERTY(QVariant currentValue READ currentValue NOTIFY currentValueChanged FINAL REVISION 14) + Q_PROPERTY(QString valueRole READ valueRole WRITE setValueRole NOTIFY valueRoleChanged FINAL REVISION 14) public: explicit QQuickComboBox(QQuickItem *parent = nullptr); @@ -114,6 +117,9 @@ public: QString textRole() const; void setTextRole(const QString &role); + QString valueRole() const; + void setValueRole(const QString &role); + QQmlComponent *delegate() const; void setDelegate(QQmlComponent *delegate); @@ -155,6 +161,11 @@ public: qreal implicitIndicatorWidth() const; qreal implicitIndicatorHeight() const; + // 2.14 (Qt 5.14) + QVariant currentValue() const; + Q_INVOKABLE QVariant valueAt(int index) const; + Q_INVOKABLE int indexOfValue(const QVariant &value) const; + public Q_SLOTS: void incrementCurrentIndex(); void decrementCurrentIndex(); @@ -189,6 +200,9 @@ Q_SIGNALS: // 2.5 (Qt 5.12) Q_REVISION(5) void implicitIndicatorWidthChanged(); Q_REVISION(5) void implicitIndicatorHeightChanged(); + // 2.14 (Qt 5.14) + Q_REVISION(14) void valueRoleChanged(); + Q_REVISION(14) void currentValueChanged(); protected: bool eventFilter(QObject *object, QEvent *event) override; |