在QML中嵌套的ListView中找不到C ++对象

时间:2013-12-02 04:31:06

标签: qml blackberry-10 blackberry-cascades

我有一个ListView嵌套在我的qml中的另一个ListView中。 ListViews都是由我从C ++类SelectRoomView获取的数据模型填充的。这是我的qml文件的代码:

import bb.cascades 1.0
import Data.SelectRoomView 1.0
import "commons"


Page {
    id: pageSelectARoom

property alias currentPage: pageSelectARoom
property string propId

attachedObjects: [
    SelectRoomView {
        id: selRoom 
    }
]

onPropIdChanged: {
    selRoom.propId = propId;
}

Container {
    layout: StackLayout {
        orientation: LayoutOrientation.TopToBottom
    }

    background: Color.create("#FFFFFF")

    ScrollView{   
        horizontalAlignment: HorizontalAlignment.Fill
        objectName: "scrollView"                   

        ListView {
            id: ruleList
            dataModel: selRoom.modelRule

            listItemComponents: [   
                ListItemComponent {
                    id: outerList

                    Container{
                        Label {
                            id: outerLabel
                            text: ListItemData.ruleName
                            textStyle.fontSize: FontSize.XXSmall
                            textStyle.color: Color.Black
                        }
                        Label{
                            id: outerId
                            text: ListItemData.ruleId
                            textStyle.fontSize: FontSize.XXSmall
                            textStyle.color: Color.Black
                            visible: true
                        }

                        ListView{
                            id: roomList
                            dataModel: selRoom.modelRoom  //I get the message ReferenceError: Can't find the variable: selRoom
                            listItemComponents: [

                                ListItemComponent {
                                    id: innerList


                                    Label{
                                        id: innerLabel
                                        text: ListItemData.name
                                        textStyle.fontSize: FontSize.XXSmall
                                        textStyle.color: Color.Black

                                    }
                                }
                            ]
                        }
                    }

                }     
            ]

        }

    }

  }
}

运行应用后,ruleName ruleList会显示innerLabel roomList。控制台显示以下消息: asset:///SelectARoom.qml:100: ReferenceError: Can't find variable: selRoom asset:///SelectARoom.qml:100: ReferenceError: Can't find variable: selRoom

即,selRoom不在roomList(嵌套列表)的范围内。 我究竟做错了什么?

2 个答案:

答案 0 :(得分:0)

我找不到ListView.listItemComponents属性的文档,但它看起来像某种委托。

如果子列表是第一个列表委托的一部分,则它可能无法访问全局变量。 委托的QML docs state只能访问其位置索引和模型的变量。

尝试访问委托范围之外的其他值,以查看是否有效

ListView {
    id: roomList
    Component.oncompleted: console.log(ruleList.height)

    ...
}

答案 1 :(得分:0)

我找到了解决方案,但效率不高。以下是新代码:

Page {
id: pageSelectARoom

property alias currentPage: pageSelectARoom
property string propId

attachedObjects: [
    SelectRoomView {
       id: selRoom 
    }
]

onPropIdChanged: {
    selRoom.propId = propId;
}

Container {
    layout: StackLayout {
    orientation: LayoutOrientation.TopToBottom
}

background: Color.create("#FFFFFF")

ScrollView{   
    horizontalAlignment: HorizontalAlignment.Fill
    objectName: "scrollView"                   

    ListView {
        id: ruleList
        dataModel: selRoom.modelRule

        function getModelMain() {           //the object 'selRoom' is accessible from this function.
             return selRoom.modelRoom;
        }

        listItemComponents: [   
            ListItemComponent {
                id: outerList

                Container{
                    id: ruleContainer

                    function getModel() {        
                        return ruleContainer.parent.getModelMain();  
                    }                       

                    Label {
                        id: outerLabel
                        text: ListItemData.ruleName
                        textStyle.fontSize: FontSize.XXSmall
                        textStyle.color: Color.Black
                    }
                    Label{
                        id: outerId
                        text: ListItemData.ruleId
                        textStyle.fontSize: FontSize.XXSmall
                        textStyle.color: Color.Black
                        visible: true
                    }

                    ListView{
                        id: roomList
                        dataModel: ruleContainer.getModel() 
                        listItemComponents: [

                            ListItemComponent {
                                id: innerList


                                Label{
                                    id: innerLabel
                                    text: ListItemData.name
                                    textStyle.fontSize: FontSize.XXSmall
                                    textStyle.color: Color.Black

                                }
                            }
                        ]
                    }
                }

            }     
        ]

    }

   }

  }
}

有更好的方法可以做到这一点,还是有其他更有效的解决方案?等待答复。感谢。