销毁设置为 ShaderEffectSource 的 sourceItem 的项目也会隐藏 ShaderEffectSource

Destroying an Item that was set as the sourceItem for a ShaderEffectSource hides the ShaderEffectSource as well

简而言之,我在做什么:

问题是当按下一个键时,两个副本都从屏幕上消失,而我希望 snapshotItem 一个保留。

注意:如果您对实现此目标的动机感兴趣,请参阅my previous question

我的代码:

import QtQuick 2.6
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480

    property int childWidth: 100
    property int childHeight: 100

    id: root
    property var dynamicItem

    Item {
        id: dummy
    }

    Component {
        id: dynamicItemComponent
        Rectangle {
            color: "red"
        }
    }

    Component.onCompleted: {
        dynamicItem = dynamicItemComponent.createObject(row);
        dynamicItem.width = childWidth;
        dynamicItem.height = childHeight;
        snapshotItem.sourceItem = dynamicItem;
        snapshotItem.scheduleUpdate();
    }

    Item {
        focus: true
        Keys.onPressed: {
            snapshotItem.sourceItem = dummy;
            dynamicItem.destroy();
        }
    }

    Row {
        id: row
        spacing: 10
        ShaderEffectSource {
            id: snapshotItem
            live: false
            width: childWidth
            height: childHeight
        }
    }
}

您不需要使用 dummyItem。您可以将 sourceItem 设置为 ShaderEffectSource 本身。

也许您应该将 recursive 设置为 true,但没有它也可以。

import QtQuick 2.6
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480

    property int childWidth: 100
    property int childHeight: 100

    id: root
    property var dynamicItem

    Component {
        id: dynamicItemComponent
        Rectangle {
            color: "red"
        }
    }

    Component.onCompleted: {
        dynamicItem = dynamicItemComponent.createObject(row);
        dynamicItem.width = childWidth;
        dynamicItem.height = childHeight;
        snapshotItem.sourceItem = dynamicItem;
        snapshotItem.scheduleUpdate();
    }

    Item {
        focus: true
        Keys.onPressed: {
            snapshotItem.sourceItem = snapshotItem;
            dynamicItem.destroy();
        }
    }

    Row {
        id: row
        spacing: 10
        ShaderEffectSource {
            id: snapshotItem
            live: false
            // recursive: sourceItem === this
            width: childWidth
            height: childHeight
        }
    }
}