diff options
author | J-P Nurmi <jpnurmi@theqtcompany.com> | 2016-01-25 17:06:30 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@theqtcompany.com> | 2016-01-26 08:21:02 +0000 |
commit | 3242a3be7c7cc84e6fcda3e90a7713d9fda16915 (patch) | |
tree | d2cfdbb78ecddfe971aa8c8e8b271c20e6f596e9 /src/templates/qquickcombobox.cpp | |
parent | 08cbefe5bb7e65ff14f8663eae53d614616f2037 (diff) |
ComboBox: add support for object array
Change-Id: Ia43ba18cfd7ce9f5c4c28e239645320af5ba41e7
Task-number: QTBUG-50141
Reviewed-by: Liang Qi <liang.qi@theqtcompany.com>
Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src/templates/qquickcombobox.cpp')
-rw-r--r-- | src/templates/qquickcombobox.cpp | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/templates/qquickcombobox.cpp b/src/templates/qquickcombobox.cpp index e3e02212..cee384e3 100644 --- a/src/templates/qquickcombobox.cpp +++ b/src/templates/qquickcombobox.cpp @@ -119,6 +119,36 @@ QT_BEGIN_NAMESPACE \sa highlightedIndex */ +class QQuickComboBoxDelegateModel : public QQmlDelegateModel +{ +public: + explicit QQuickComboBoxDelegateModel(QQuickComboBox *combo); + QString stringValue(int index, const QString &role) Q_DECL_OVERRIDE; + +private: + QQuickComboBox *combo; +}; + +QQuickComboBoxDelegateModel::QQuickComboBoxDelegateModel(QQuickComboBox *combo) : + QQmlDelegateModel(qmlContext(combo), combo), combo(combo) +{ +} + +QString QQuickComboBoxDelegateModel::stringValue(int index, const QString &role) +{ + QVariant model = combo->model(); + if (model.userType() == QMetaType::QVariantList) { + QVariant object = model.toList().value(index); + if (object.userType() == QMetaType::QVariantMap) { + QVariantMap data = object.toMap(); + if (data.count() == 1 && role == QLatin1String("modelData")) + return data.first().toString(); + return data.value(role).toString(); + } + } + return QQmlDelegateModel::stringValue(index, role); +} + class QQuickComboBoxPrivate : public QQuickControlPrivate { Q_DECLARE_PUBLIC(QQuickComboBox) @@ -302,7 +332,7 @@ void QQuickComboBoxPrivate::createDelegateModel() delegateModel = model.value<QQmlInstanceModel *>(); if (!delegateModel && model.isValid()) { - QQmlDelegateModel *dataModel = new QQmlDelegateModel(qmlContext(q), q); + QQmlDelegateModel *dataModel = new QQuickComboBoxDelegateModel(q); dataModel->setModel(model); dataModel->setDelegate(delegate); if (q->isComponentComplete()) |