简体   繁体   English

Qt:如何获取QQuickItem的快照,而不从获取的结果中删除其子级QQuickItems

[英]Qt : How to grab a snapshot of a QQuickItem leaving out its child QQuickItems from the grabbed result

My question is a follow up on this discussion . 我的问题是对该讨论进行跟进。

Yes . 是的 Following way of grabToImage can get me a snapshot of any particular QQuickItem like parent_rect below. 以下的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");
                   });

Problem: 问题:
But this grabToImage gets me the snapshot of the all its children as well namely child_rect1 and child_rect2 . 但是,这个grabToImage我获取了所有子grabToImage的快照,即child_rect1child_rect2

Question: 题:
How can I get the snapshot of parent_rect only without getting its children add into the returned result? 如何仅获取parent_rect的快照而不将其子级添加到返回结果中?

One possible solution is to hide the children and then restore visibility. 一种可能的解决方案是隐藏子级,然后恢复可见性。

Example: 例:

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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM