Qt:如何抓取QQuickItem的快照,从抓取的结果中忽略其子QQuickItems
我的问题是后续问题 是。下面的Qt:如何抓取QQuickItem的快照,从抓取的结果中忽略其子QQuickItems,qt,qml,qtquick2,qimage,qquickitem,Qt,Qml,Qtquick2,Qimage,Qquickitem,我的问题是后续问题 是。下面的grabToImage方法可以获得任何特定QQuickItem的快照,如下面的parent\rect Rectangle { id: parent_rect width: 400 height: 400 Rectangle { id: child_rect1 width: parent.width/4 height: parent.height/4 } Rectang
grabToImage
方法可以获得任何特定QQuickItem
的快照,如下面的parent\rect
Rectangle {
id: parent_rect
width: 400
height: 400
Rectangle {
id: child_rect1
width: parent.width/4
height: parent.height/4
}
Rectangle {
id: child_rect2
width: parent.width/4
height: parent.height/4
}
}
// ...
parent_rect.grabToImage(function(result) {
result.saveToFile("something.png");
});
问题:但这个
grabToImage
可以让我看到它所有子对象的快照,也就是child\u rect1
和child\u rect2
问题:如何仅获取父对象的快照而不将其子对象添加到返回结果中?一种可能的解决方案是隐藏子对象,然后恢复可见性 例如:
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")
}
好啊至少这是一种方式。Qt真的应该向QQuickItem类添加一个API,而无需做太多工作就可以创建这个层快照。我对文档的搜索一开始就期望这是一件非常正常的事情。谢谢@eyllanesc@Game_Of_Threads应该,但似乎在他们的需求范围内没有。我的解决方案很简单,所以我认为已经足够了。如果我能帮助你,你可以投一票或者把它标为正确:)当然可以@eyllanesc