Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved] change external data model in qml
Forum Update on Monday, May 27th 2025

[solved] change external data model in qml

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 5.6k 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.
  • J Offline
    J Offline
    jimon
    wrote on 7 Mar 2012, 23:11 last edited by
    #1

    Hello,

    I created model in c++ code, something like this :

    @
    class EditorNode : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    ...
    }

    QVariantList test;
    test << QVariant::fromValue((QObject*)new EditorNode());
    connect(((EditorNode*)test.at(0).value<QObject*>()), SIGNAL(nameChanged()), this, SLOT(test()));
    qmlView->rootContext()->setContextProperty("nodesList", test);
    @

    and I'm trying to change data from qml

    @
    Repeater
    {
    model: nodesList;
    delegate: Rectangle
    {
    width:80; height:50;
    TextEdit
    {
    anchors.fill: parent;
    text:model.modelData.name; // actually this should be binding, not copying
    }
    }
    }
    @

    but when I change text in textview, nothing happens to model (dont recieve slot call), how can I solve this problem ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jimon
      wrote on 8 Mar 2012, 23:36 last edited by
      #2

      well, I have tried different approaches and came to the problem with MVC pattern, and property bindings in qml

      example :
      @
      // some really simple code
      Item
      {
      property int a: 1; // model
      property int b: a; // data in view (for example we bind text string to text property in textview)
      onAChanged: {console.log("a changed to ", a);}
      Component.onCompleted:
      {
      console.log("change model directly : ");
      a = 2;
      console.log("change model indirectly through binding : ");
      b = 4; // I really want get model data here, but this line only do assignment to property in view part
      }
      }
      @

      I have not found any mechanism for obtaining the primary source of data, and only one approach works :

      @
      Item
      {
      property int a: 1;
      property int b: 1;
      onAChanged: {console.log("a changed to ", a);}
      onBChanged: {a = b;}
      Component.onCompleted:
      {
      console.log("change model directly : ");
      a = 2;
      console.log("change model indirectly through binding : ");
      b = 4;
      }
      }
      @

      and this how use it with data models
      @
      ListModel
      {
      id: foo;
      ListElement {a: 2;}
      }

      Repeater
      {
      model: foo;
      delegate: Item
      {
      property int b: 0;
      onBChanged: {foo.setProperty(index, "a", b);}

      Component.onCompleted:
      {
      b = 1;
      }
      }
      }
      @

      but why model delegate (view-controller) must acts as a data-source "proxy" by default ? when in most cases when we change "proxy" property we just need to change primary source of data without knowing it's name
      It shouldn't be that hard, because internal binding mechanism in qt contains all information for getting data source, is there any simple way of doing this ? just something like Qt.getBindingOrigin(b) that return id a will work

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jimon
        wrote on 10 Mar 2012, 18:35 last edited by
        #3

        so, solution : weak link by hand :)

        @
        Repeater
        {
        model: nodesList;
        delegate: Rectangle
        {
        width:80; height:50;
        TextEdit
        {
        anchors.fill: parent;
        onTextChanged: {nodesList[index].name = text;}
        Component.onCompleted: {text = nodesList[index].name;}
        }
        }
        }
        @

        ps. please close the thread, thanks

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Eddy
          wrote on 10 Mar 2012, 18:43 last edited by
          #4

          Hi jimon,

          Thanks for posting your solution.

          Btw you can mark your post as solved by editing your first post and add [solved] to the title.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0

          1/4

          7 Mar 2012, 23:11

          • Login

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