Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QML Binding with Dynamic Property of QList<QObject*> type

QML Binding with Dynamic Property of QList<QObject*> type

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dong
    wrote on 4 Dec 2015, 07:26 last edited by Dong 12 Apr 2015, 09:31
    #1

    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*> type

    Any 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");
                    }
    }
    
    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dong
      wrote on 4 Dec 2015, 10:18 last edited by
      #2

      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");
          }
      
      1 Reply Last reply
      0

      2/2

      4 Dec 2015, 10:18

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved