我想循环遍历 QML 网格的子级并使用 Javascript 销毁它们中的每一个。
Grid {
id: contentGrid
spacing: 10
ImageItem { imageSource: "file:/foo.jpeg" } // destroy this
ImageItem { imageSource: "file:/bar.jpeg" } // destroy this as well
}
我尝试做类似的事情,但到目前为止还没有成功。
for(var i = 0; contentGrid.children.length() < i; i++) {
contentGrid.childAt(i).destroy();
}
在上面的尝试中您遇到了许多问题...首先,您需要向后迭代,因为您将在前进时向下移动子级的内容(即,如果您删除#1,则数字#2将成为孩子#1,然后你去删除#2,这将是老孩子#3)。
其次,你需要以不同的方式接触孩子。 childAt() 函数用于将子项定位在屏幕上的特定 x,y 处,而不是列表中的位置。
试试这个:
import QtQuick 1.0
Rectangle {
width: 400
height: 400
Grid {
id: contentGrid
spacing: 10
Text { text: "foo" } // destroy this
Text { text: "bar" } // destroy this as well
}
MouseArea {
anchors.fill: parent
onClicked: {
for(var i = contentGrid.children.length; i > 0 ; i--) {
console.log("destroying: " + i)
contentGrid.children[i-1].destroy()
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)