Loader in 4.7.2



  • 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;
      }
     }
    ]
    

    }
    }
    }
    }

    @



  • 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



  • Thanks Michael.

    That seems to work for Qt 4.7.1 and Qt 4.7.2



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.