Add attributes in qml delegate to access this holder (reprinted)

Source: Internet
Author: User

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 才行

Add attributes in qml delegate to access this holder (reprinted)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.