QML Delegate中访问该持有者的方式 附加属性(转载)

http://blog.csdn.net/yuxiaohen/article/details/17226971
 
用法很奇葩记录一下,实测可以,用于弱化delegate与持有者的依赖
delegate: Component {
            Rectangle {
                id:wrapper
                width: parent.width;
                height: 30;
                BorderImage {
                    id:bg_element
//                    source: wrapper.isCurrent
                    anchors.fill:parent;
                    border.left: 5; border.top: 5
                    border.right: 5; border.bottom: 5
                }
                Text {
                    anchors.left: parent.left;
                    anchors.verticalCenter: parent.verticalCenter;
                    anchors.leftMargin: parent.width * 0.6;
                    verticalAlignment: Text.AlignVCenter;
                    font.family: "SimSun";
                    text: people_name;
                }
                MouseArea {
                    anchors.fill: parent;
                    hoverEnabled: true;
                    onEntered: {
                        console.log("enter");
                        bg_element.source = "qrc:/images/images/pointer_hover.png";
                    }
                    onExited: {
                        console.log("exit");
                        bg_element.source = ""
                    }
                    onClicked: {
                        console.log("index",index);
                        wrapper.ListView.view.currentIndex = index;
                        console.log("current index",wrapper.ListView.view.currentIndex);
                        console.log("is current item",wrapper.ListView.isCurrentItem);
                        console.log("model:",wrapper.ListView.view.model);
                        console.log("clicked");
                        bg_element.source = "qrc:/images/images/pointer_clicked.png";
                    }
                }
            }
        }
这里涉及到附加属性和属性的概念,访问附加属性时不需要element实体,感觉有点像使用class的static成员,而属性是需要element实体的.
譬如如上代码,delegate是个component,在Rectangle(也就是id为wrapper)这一作用域,才可直接访问ListView的属性(可能附加属性也是如此,没深究)
要访问的isCurrentItem是ListView的附加属性,则 wrapper.ListView.isCurrentItem 即可
要访问的currentIndex是ListView的属性,则 wrapper.ListView.view.currentIndex 才行
posted @ 2014-10-10 13:36  ZN大叔  阅读(2931)  评论(0编辑  收藏  举报