QML Delegate中訪問該持有人的方式 附加屬性(轉載)

來源:互聯網
上載者:User

標籤:blog   http   使用   ar   sp   art   on   log   代碼   

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

QML Delegate中訪問該持有人的方式 附加屬性(轉載)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.