QML访问子项内容

访问Repeater子项

复制代码
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.14

Window {
    width: 600
    height: 400
    visible: true
    title: "Loader + Component 原理演示"

    Repeater {
        id: rep
        model: 3
        Button {
            y: index * 60
            text: "btn" + index
        }
    }

    Button {
        id: btn2
        x: 200
        onClicked: {
            for (var i = 0; i < rep.count; i++) {
                if (rep.itemAt(i) instanceof Button) {
                    console.log(rep.itemAt(i).text)
                }
            }
        }
    }
}

if (rep.itemAt(i) instanceof Button) {

console.log(rep.itemAt(i).text)

}

Repeater的itemAt方法访问子项,这里子项类型为Button,可以打印处Button的text属性

访问Column布局的子项(一)

复制代码
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.14

Window {
    width: 600
    height: 400
    visible: true
    title: "Loader + Component 原理演示"
    
    Column {
        id: col
        spacing: 20
        Button {
            id: btn
            text: "btn"
        }
        Text {
            id: text
            text: qsTr("text")
        }
        Rectangle {
            id: rect
            width: 100
            height: 200
            color: "black"
        }
    }
    Button {
        id: btn2
        x: 200
        onClicked: {
            console.log(col.children.length)
            for (var i = 0; i < col.children.length; i++) {
                if (col.children[i] instanceof Button
                        || col.children[i] instanceof Text) {
                    console.log(col.children[i].text)
                }
            }
        }
    }
}

如上Column布局下有Button、Text、Rectangle, 外部访问可

  • col.children.length返回column布局下有多少item
  • col.childreni instanceof Button判断column子控件是否为Button

访问Column布局的子项(二)

bash 复制代码
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.14

Window {
    width: 600
    height: 400
    visible: true
    title: "Loader + Component 原理演示"

    Column {
        id: col
        spacing: 20
        Repeater {
            id: rep
            model: 3
            Button {
                y: index * 60
                text: "btn" + index
            }
        }
        Text {
            id: text
            text: qsTr("text")
        }
        Rectangle {
            id: rect
            width: 100
            height: 200
            color: "black"
        }
    }
    Button {
        id: btn2
        x: 200
        onClicked: {
            console.log(col.children.length)
            for (var i = 0; i < col.children.length; i++) {
                console.log(col.children[i])

                //                if (col.children[i] instanceof Button
                //                        || col.children[i] instanceof Text) {
                //                    console.log(col.children[i].text)
                //                }
            }
        }
    }
}

如上Column布局下有Repeater(包含3个Button)、Text、Rectangle, 外部访问可

bash 复制代码
Button {
        id: btn2
        x: 200
        onClicked: {
            console.log(col.children.length)
            for (var i = 0; i < col.children.length; i++) {
                console.log(col.children[i])

                //                if (col.children[i] instanceof Button
                //                        || col.children[i] instanceof Text) {
                //                    console.log(col.children[i].text)
                //                }
            }
        }
    }

如上运行结果

bash 复制代码
qml: 6
qml: Button_QMLTYPE_0(0x1e5eb1daa90)
qml: Button_QMLTYPE_0(0x1e5eb1daf40)
qml: Button_QMLTYPE_0(0x1e5eb1da220)
qml: QQuickRepeater(0x1e5e87374c0)
qml: QQuickText(0x1e5e8736a70)
qml: QQuickRectangle(0x1e5e8736890)

包含6个item,其中包含3个Button、1个Repeater、1个Text和1个Rectangle

访问ListView子项(一)

该场景是ListView的delegate子项为Text情况

bash 复制代码
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.14

Window {
    width: 600
    height: 400
    visible: true
    title: "Loader + Component 原理演示"
    ListView {
        id: listView
        width: 00
        height: 300
        model: ["zhangsan", "lisi", "wangwu"]
        delegate: Text {
            text: modelData
        }
    }
    Button {
        id: btn2
        x: 200
        onClicked: {
            console.log(listView.contentItem.children.length)
            for (var i = 0; i < listView.contentItem.children.length; i++) {
                if (listView.contentItem.children[i] instanceof Text) {
                    console.log(listView.contentItem.children[i].text)
                }
            }
        }
    }
}

访问ListView子项(二)

bash 复制代码
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.14

Window {
    width: 600
    height: 400
    visible: true
    title: "Loader + Component 原理演示"

    ListView {
        id: listView
        width: 00
        height: 300
        model: ["zhangsan", "lisi", "wangwu"]
        delegate: Column {
            Text {
                text: modelData + " txt"
            }
            Button {
                text: modelData + " btn"
            }
        }
    }
    Button {
        id: btn2
        x: 200
        onClicked: {
            console.log(listView.contentItem.children.length)
            for (var i = 0; i < listView.contentItem.children.length; i++) {
                var col = listView.contentItem.children[i]
                for (var j = 0; j < col.children.length; j++) {
                    console.log(col.children[j].text)
                }
            }
        }
    }
}

如上该场景是ListView的delegate子项为Column布局场景

bash 复制代码
for (var i = 0; i < listView.contentItem.children.length; i++) {
                var col = listView.contentItem.children[i]
                for (var j = 0; j < col.children.length; j++) {
                    console.log(col.children[j].text)
                }
            }

先获取col布局listView.contentItem.childreni,在对column布局访问其子项

相关推荐
xy345343 分钟前
Axure 9.0 中继器核心结构
前端·ui·html·axure·原型·产品设计
老王以为1 小时前
走进 AI Agent 第四篇(上):知识获取管道——RAG 基础
前端·人工智能·全栈
夏天要喝冰可乐1 小时前
一处写作,多平台分发:我给掘金做了一款开源 Chrome 插件
前端·chrome·vibecoding
货拉拉技术1 小时前
货拉拉开源Hadice:安卓 & 鸿蒙桌面调试工具
前端
程序猿DD2 小时前
OctaFuse Gateway 2.11.0:流式请求优化、用户折扣策略与错误契约升级
前端·后端
尾善爱看海2 小时前
《JavaScript 数组操作全攻略:12 类 API + 30 个实战场景 + 20 个避坑指南》
前端·javascript·面试
计算机魔术师2 小时前
Anthropic 估值冲 4 万亿,但企业客户已经偷偷换了便宜模型
前端
OpsEye2 小时前
为什么你的 Agent 莫名烧钱?聊聊循环调用的兜底方案
javascript·ai编程
王六米。2 小时前
RAG知识库建设 文档解析 切分与索引如何衔接
服务器·前端·网络·企业数字化转型·智钳智能盒子