代码之家  ›  专栏  ›  技术社区  ›  TheWaterProgrammer

qt:如何抓取qquickitem的快照,从抓取的结果中去掉其子qquickitems

  •  0
  • TheWaterProgrammer  · 技术社区  · 7 年前

    我的问题是跟进 this discussion 是的。

    是的 .遵循的方式 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");
                       });
    

    问题:
    但是这个 抓斗图像 给我所有孩子的快照 child_rect1 child_rect2 是的。

    问题:
    如何获取的快照 父目录 只在返回的结果中没有添加子项?

    1 回复  |  直到 7 年前
        1
  •  1
  •   eyllanesc    7 年前

    一种可能的解决方案是隐藏子对象,然后恢复可见性。

    例子:

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