一种可能的解决方案是隐藏子对象,然后恢复可见性。
例子:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
function grabWithoutChildren(item, filename){
var isVisibleList = []
var i
for(i in item.children){
isVisibleList[i] = item.children[i].visible
item.children[i].visible = false
}
item.grabToImage(function(result) {
result.saveToFile(filename)
for(i in item.children){
item.children[i].visible = isVisibleList[i]
}
})
}
Rectangle {
id: parent_rect
width: 400
height: 400
color: "red"
Rectangle {
id: child_rect1
width: parent.width/4
height: parent.height/4
color: "blue"
}
Rectangle {
id: child_rect2
x: 10
y: 10
width: parent.width/4
height: parent.height/4
color: "green"
Rectangle{
x:50
y:50
width: 100
height: 100
color: "white"
}
}
}
Component.onCompleted: grabWithoutChildren(parent_rect, "something.png")
}