QML Binding with Dynamic Property of QList<QObject*> type
-
What I'm trying to do is binding with Dynamic Property and this is my code
In this sample:
- I can binding TextField.text with sampleText function which return value of QString type
- But I can't binding with TableView.model with "tableviewItemSource" which return value of QList<DynamicObject*> typeAny one know why it can't binding with QList<DynamicObject*> ? and how to make it work?
DynamicObject.h
class DynamicObject : public QObject { Q_OBJECT public: DynamicObject(QObject *parent = 0); Q_INVOKABLE QVariant binding(const QString &propName) const; Q_INVOKABLE QList<DynamicObject*> bindingQList(const QString &propName) const; Q_INVOKABLE QString sampleText(const QString &inString) const; }
DynamicObject.cpp
DynamicObject::DynamicObject(QObject *parent) { } QVariant DynamicObject::binding(const QString &propName) const { QVariant result = this->property(propName.toStdString().c_str()); return result; } QList<DynamicObject*> DynamicObject::bindingQList(const QString &propName) const { QList<DynamicObject*> result = this->property(propName.toStdString().c_str()).value<QList<DynamicObject*> >(); return result; } QString DynamicObject::sampleText(const QString &inString) const { return inString; }
main.cpp
int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; DatacontextController datacontextController ; DynamicObject* dynamicObject = datacontextController.ReadMetadataToDynamicObject(formMetadata); QQmlContext* ctx = engine.rootContext(); ctx->setContextObject(dynamicObject); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec();
main.qml
ApplicationWindow { id:root visible: true width: 1024 height: 768 title: qsTr("Test Application") TableView { id: tblTableView anchors.fill: parent //parent: rowLayout1 Layout.fillWidth: true Layout.fillHeight: true TableViewColumn { id:colName title: "Name" width: 150 delegate: Text{ text: modelData.binding("Name") } model: bindingQList("tableviewItemSource"); } TextField{ anchors.margins: 100 id:txtName text : sampleText("Hello Wolrd"); } }
-
I found the problem why it cannot binding with QList<DynamicObject*> on this link
even thought that DynamicObject is inherited from QObject* but Qml need the the model to be QList<Object*>
and each item in the model is DynamicObject* instance.This is my complete Sample
DynamicObject.h
#ifndef DYNAMICOBJECT_H #define DYNAMICOBJECT_H #include <QObject> #include <QString> #include <QVariant> class DynamicObject : public QObject { Q_OBJECT public: DynamicObject(QObject *parent = 0); Q_INVOKABLE QVariant binding(const QString &propName) const; }
DynamicObject.cpp
#include "dynamicobject.h" DynamicObject::DynamicObject(QObject *parent) { } QVariant DynamicObject::binding(const QString &propName) const { QVariant result = this->property(propName.toStdString().c_str()); return result; }
main.cpp
int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; DatacontextController datacontextController ; DynamicObject* dynamicObject = datacontextController.ReadMetadataToDynamicObject(formMetadata); QQmlContext* ctx = engine.rootContext(); ctx->setContextObject(dynamicObject); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
main.qml
ApplicationWindow { id:root visible: true width: 1024 height: 768 title: qsTr("Test Application") TableView { id: tblTableView anchors.fill: parent //parent: rowLayout1 Layout.fillWidth: true Layout.fillHeight: true TableViewColumn { id:colName title: "Name" width: 150 delegate: Text{ text: model.modelData.binding("Name") } model: binding("tableviewItemSource"); }