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. Loader in 4.7.2
Forum Updated to NodeBB v4.3 + New Features

Loader in 4.7.2

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.1k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    bundickt
    wrote on 9 Mar 2011, 00:18 last edited by
    #1

    I want to create a row of items based on a model. The items may be different based on its type attribute in the model. I am able to do this using a loader in Qt 4.7.1, but it only sort of works in Qt 4.7.2. I was wondering if anyone had any idea on how to solve this but not break the new rules defined in Qt 4.7.2.

    The problem I am getting in Qt 4.7.2 is the model data is not visible in my Components (type1 and type2); it complains it "can't find variable: name".

    Example (work perfectly in Qt 4.7.1):
    @
    Rectangle {
    width: 800
    height: 400

    ListModel {
    id: myModel;

    ListElement {
    type: 1;
    name: "Name 1";
    }

    ListElement {
    type: 2;
    name: "Name 2";
    }

    ListElement {
    type: 1;
    name: "Name 3";
    }

    ListElement {
    type: 2;
    name: "Name 4";
    }

    ListElement {
    type: 2;
    name: "Name 5";
    }
    }

    Component {
    id: type1;

    Item {
    width: 100;
    height: 100;

    Rectangle {
    anchors.fill: parent;
    anchors.margins: 10;

    color: "silver";
    radius: 10;
    
    Text {
     text: name;
     anchors.centerIn: parent;
     color: "black";
    }
    

    }
    }
    }

    Component {
    id: type2;

    Item {
    width: 100;
    height: 100;

    Rectangle {
    anchors.fill: parent;
    anchors.margins: 10;

    color: "black";
    radius: 10;
    
    Text {
     text: name;
     anchors.centerIn: parent;
     color: "white";
    }
    

    }
    }
    }

    Row {
    id: myRow;
    anchors.horizontalCenter: parent.horizontalCenter

    Repeater {
    model: myModel;

    Loader {
    id: myLoader;

    states: [
     State {
      name: "type1"
      when: type == 1
      PropertyChanges {
       target: myLoader;
       sourceComponent: type1;
      }
     },
     State {
      name: "type2"
      when: type == 2
      PropertyChanges {
       target: myLoader;
       sourceComponent: type2;
      }
     }
    ]
    

    }
    }
    }
    }

    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on 9 Mar 2011, 01:21 last edited by
      #2

      Hi,

      You can add

      @property string name: model.name@

      to the Loader, which should work in both 4.7.1 and 4.7.2.

      It was likely the fix for http://bugreports.qt.nokia.com/browse/QTBUG-13481 which changed this behavior. I'll follow up to see if I can find any more details.

      Regards,
      Michael

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bundickt
        wrote on 9 Mar 2011, 01:30 last edited by
        #3

        Thanks Michael.

        That seems to work for Qt 4.7.1 and Qt 4.7.2

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on 9 Mar 2011, 04:00 last edited by
          #4

          Other options to get this working:

          • Make the components children of the Loader
          • Define each component in a file (rather than inline)

          QTBUG-13481 changed the way the context lookup is done, so that inline components used by Loader inherit the context of where they are defined (rather than the Loader's context). This is considered "more correct", and should be documented (I've created "QTBUG-18011":http://bugreports.qt.nokia.com/browse/QTBUG-18011 for this)

          Regards,
          Michael

          1 Reply Last reply
          0

          3/4

          9 Mar 2011, 01:30

          • Login

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