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

相关推荐
三天不学习22 分钟前
Egg.js 4 突然爆火,原因是否归结于AI 原生落地需求爆发?
前端·javascript·全栈·egg.js
xcs194051 小时前
前端 vue 的前端页面debugger 进不去
前端·javascript·vue.js
明月_清风2 小时前
Deno 终局来了:从挑战 Node 到被 Cloudflare 收编
前端·后端·node.js
Csvn3 小时前
框架性能优化
前端
回眸&啤酒鸭5 小时前
【回眸】OpenSwarm 多智能体协作系统实战指南
大数据·前端·人工智能
用户69371750013845 小时前
2026,程序员的时代拐点到了
android·前端·后端
大龄秃头程序员5 小时前
一次 iBeacon + BLE 无感解锁方案的实现记录
前端
小兔子5 小时前
Python 的 GIL 与 free-threading:3.13 之后「去 GIL」走到哪一步了
前端
IT_陈寒5 小时前
SpringBoot自动配置差点让我加班到凌晨
前端·人工智能·后端
guslegend6 小时前
脚手架原理与本地调试:从 bin 软链接到 npm link
前端·npm·node.js·脚手架·前端工程化