【问题标题】:Destroying an Item that was set as the sourceItem for a ShaderEffectSource hides the ShaderEffectSource as well销毁设置为 ShaderEffectSource 的 sourceItem 的项目也会隐藏 ShaderEffectSource
【发布时间】:2017-03-10 15:51:15
【问题描述】:

简而言之,我在做什么:

  • 我有一个名为snapshotItemlive: falseShaderEffectSource 项目
  • 动态实例化一个名为dynamicItem的项目
  • 设置snapshotItem.sourceItem = dynamicItem
  • 致电snapshotItem.scheduleUpdate()
  • 至此,我成功在屏幕上看到了dynamicItem的两个副本
  • 在任何键上,我:
    • snapshotItem.sourceItem 设置为一个空的虚拟项目,以减少下一步导致问题的可能性
    • 摧毁dynamicItem

问题是当按下一个键时,两个副本都会从屏幕上消失,而我希望 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
        }
    }
}

【问题讨论】:

  • 遵循简单的逻辑,禁用 live 并分配一个虚拟对象就足以让它按照您想要的方式工作,事实上,仅第一件事就足够了。然而,这就是 Qt,它并不总是遵循逻辑,实际上很多时候它并不遵循逻辑。随意提交错误报告,并可能在多年后看到此修复:D

标签: qt qml qtquick2


【解决方案1】:

您不需要使用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
        }
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多