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布局访问其子项

相关推荐
Larcher3 小时前
React Router 不只是页面跳转:从 SPA 路由到权限守卫的完整实践
javascript·后端
Larcher3 小时前
大模型为什么每次回答都不一样?一文搞懂 Temperature、Top K 与 Top P
javascript·后端
用户938515635076 小时前
从零在浏览器里跑 DeepSeek-R1:WebGPU + Transformer.js 全链路实战
前端·设计模式·typescript
CodeSheep6 小时前
稚晖君公司人事大变动,来了!
前端·后端·程序员
鱼樱前端7 小时前
用 AI 做内容变收入
前端·人工智能·ai编程
eric-sjq7 小时前
10 分钟实战:用 0.6B 本地模型生成中文网页(WanlyFrontend + 编译器全流程)
javascript·机器学习·自然语言处理·html
浮生望7 小时前
React + TypeScript 组件设计进阶:从类型约束到无状态组件的三次进化
前端
To_OC7 小时前
LC 438 找到所有字母异位词:暴力超时后,我靠滑动窗口一招搞定
javascript·算法·leetcode
90后的晨仔7 小时前
Mac 开发 uni-app 终极方案:让安卓模拟器彻底脱离 Android Studio 独立运行
前端·vue.js
90后的晨仔7 小时前
别再搞混了!uni-app 中 node_modules 和 uni_modules 到底是什么关系?
前端