[Qt-qml] Loaders, Components and ListViews

Christoper Ham christopher.ham at nokia.com
Thu Dec 16 02:26:54 CET 2010


On 12/16/2010 11:11 AM, ext Bartosh Wroblevksy wrote:
>
>
>
>
>
>   So I have this little example where data of a ListElement determines what component to load in a list view something like this
>
>
>
>
> ListView {
>
> ListModel {
>      id: aModel
>      ListElement {
>          name: "Bill Smith"
>          componentID: "redRectangle"
>          number: "555 3264"
>      }
>      ListElement {
>          name: "John Brown"
>          componentID:"brownRectangle"
>          number: "555 8426"
>      }
>      ListElement {
>          name: "Sam Wise"
>          componentID: "yellowRectangle"
>          number: "555 0473"
>      }
> }
>
>      Component{
>          id: redRectangle
>          Rectangle{
>          width: 180; height: 200
>          color:"red"
>          }
>      }
>
>      Component{
>          id: brownRectangle
>          Rectangle{
>          width: 180; height: 200
>          color:"brown"
>          }
>      }
>
>      Component{
>          id: yellowRectangle
>          Rectangle{
>          width: 180; height: 200
>          color:"yellow"
>          }
>      }
>
>      width: 180; height: 600
>      model: aModel
>      delegate:Loader{
>          id:myLoader
>          sourceComponent: (componentID == "redRectangle")? redRectangle
>          : (componentID == "yellowRectangle"? yellowRectangle
>          : brownRectangle)
>      }
> }
> This all works fine and nice. But I was wondering if
>          sourceComponent: (componentID == "redRectangle")? redRectangle        : (componentID == "yellowRectangle"? yellowRectangle        : brownRectangle)
> could perhaps be more generic without all the nested stuff. Something that would ideally look like this
>          sourceComponent: componentID
> Any input would be great. Otherwise QML is fun, fun, fun.
>
> Thanks,Bartosh
>
>
>
>   		 	   		
>    
>
>
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
>    
Hi Bartosh,

It wouldn't compress the express to a single line, but to do it without 
nesting you can use:

sourceComponent: {
     switch (componentID) {
     case "redRectangle":
         redRectangle;
         break;
     case "yellowRectangle":
         yellowRectangle;
         break;
     ...

etc.

If you created a function that would lighten up the code a bit.

Br, Christopher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20101216/05f5b8b2/attachment.html 


More information about the Qt-qml mailing list