Qt_Note21_QML_访问复杂控件中的组件

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    color: "white"

    ListView{
        id: list
        width: 100
        height: 300
        model: ["zhangsan", "lisi", "wangwu"]   //quan'bu'sh够

//        delegate: Text {    //绘制 控制单个 项
//            id: txt
//            text: modelData
//        }

        delegate: Column{
            Text {
                id: txt
                text: modelData + "text"
            }
            Button {
                text: modelData + "btn"
            }
        }

    }

    Button{
        id: btn2
        x: 200
        text: "clicked"
        onClicked: {
//            console.log(txt.text)
//            console.log(list.contentItem.children.length)
            for(var i = 0; i < list.contentItem.children.length; i++)
            {
//                console.log(list.contentItem.children[i])
//                if(list.contentItem.children[i] instanceof Text)
//                    console.log(list.contentItem.children[i].text)

                var col = list.contentItem.children[i]
                for(var j = 0; j < col.children.length; j++){
                    console.log(col.children[j])
                }
            }
        }
    }

    Repeater {
        id: rep
        model: 3    //Repeater內部控件是通过model自动生成的,不能判断每个button的id是什么,不能直接访问id
        Button {
            id: btn
            y: index * 60
            text: "btn" + index
        }
    }

//    Button {
//        id: btn2
//        x: 200
//        text: "clicked!"
//        onClicked: {
//            // 打印Repeater三个button的text
            console.log("hello")
            console.log(rep.itemAt(0).text)
            for(var i = 0; i < rep.count; i++)
            {
                console.log(rep.itemAt(i).text)
            }

//            // 打印column里的属性
            console.log(btn.text, txt.text, rect.height, '\n')
//            console.log(col.children.length)
//            for(var i = 0; i < col.children.length; i++)
//            {
                console.log(col.children[i].text)
                console.log(col.children[i] instanceof Button)  //判断当前项是不是我们寻找的控件
//                if(col.children[i] instanceof Button)
//                    console.log(col.children[i].text)
//            }
//        }
//    }

//    Column {
//        id: col
//        spacing: 20
        Button {
            id: btn
            text:  "btn"
        }
        Text {
            id: txt
            text: qsTr("txt")
        }
        Rectangle{
            id: rect
            width: 100
            height: 20
            color: "black"
        }

//        Repeater {  //Repeater也是一个控件
//            id: rep
//            model: 3
//            Button {
//                id: btn
//                text: "btn" + index
//            }
//        }
//        Text {
//            id: txt
//            text: qsTr("txt")
//        }
//        Rectangle{
//            id: rect
//            width: 100
//            height: 20
//            color: "black"
//        }

//    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可可西里啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值