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"
// }
// }
}
Qt_Note21_QML_访问复杂控件中的组件
于 2024-04-05 22:39:17 首次发布