QML 中如何动态创建组件_billy的博客
qml动态创建模型及代理中的代理_蓝博皙的博客-
QML ListView 拖拽移动,代理和模型 - Cpper - C++博客
20.Quick QML-Flickable滑动窗口 - 云+社区 - 腾讯云
Qml Flickable_quietbxj的博客
MyItem.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
Rectangle {
id: rec
property alias textInformation: myText.text
signal destroyMyself(var object)
Text {
id: myText
anchors.left: parent.left
anchors.leftMargin: 10
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 24
font.family: "微软雅黑"
color: "black"
}
Button {
anchors.right: parent.right
anchors.rightMargin: 10
anchors.verticalCenter: parent.verticalCenter
text: "delete"
onClicked: {
destroyMyself(rec)
}
}
}
model.qml
import QtQuick 2.2标签:anchors,parent,动态创建,height,width,QML,组件,id,row From: https://blog.51cto.com/u_15930680/5990926
Item {
width: 200
height: 50
ListModel {
id: myModel
ListElement {
type: "Dog"
age: 8
cnt: 3
}
ListElement {
type: "Cat"
age: 5
cnt: 5
}
}
Component {
id: myDelegate
Flickable {
width: listView.width
height: row.height
contentWidth: row.width
contentHeight: row.height
Row {
id: row
spacing: 2
Text {
text: type + ", " + age
font.pointSize: 12
}
Component.onCompleted: {
for (var i = 0; i < cnt; ++i) {
createObject(row)
}
}
}
}
}
ListView {
id: listView
spacing: 5
anchors.fill: parent
model: myModel
delegate: myDelegate
}
Component.onCompleted: {
console.log("ListModel", myModel)
console.log("Component", myDelegate)
}
function componentDestroy(object) {
object.destroy()
}
function createObject(parent) {
// createComponent from external file "MyItem.qml"
var component = Qt.createComponent("MyItem.qml")
if (component.status === Component.Ready) {
var obj = component.createObject(parent, {
"color": "yellow",
"width": 150,
"height": 150,
"textInformation": 'x'
})
}
}
function createQmlObject(parent) {
var newObject = Qt.createQmlObject(
'import QtQuick 2.0; Rectangle {color: "blue"; width: 1024; height: 50}',
parent, "")
}
}