本文介绍了销毁设置为 ShaderEffectSource 的 sourceItem 的 Item 也会隐藏 ShaderEffectSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简而言之,我在做什么:
What I'm doing, in short:
- 我有一个
ShaderEffectSource
项目,名为snapshotItem
,带有live: false
- 动态实例化一个名为
dynamicItem
的项目 - 设置
snapshotItem.sourceItem = dynamicItem
- 调用
snapshotItem.scheduleUpdate()
- 此时,我成功地在屏幕上看到了
dynamicItem
的两个副本 - 在任意键上,我:
- 将
snapshotItem.sourceItem
设置为一个空的、虚拟的 Item,以减少下一步出现问题的可能性 - 销毁
dynamicItem
- I have a
ShaderEffectSource
Item namedsnapshotItem
withlive: false
- Dynamically instantiating an Item called
dynamicItem
- Setting
snapshotItem.sourceItem = dynamicItem
- Calling
snapshotItem.scheduleUpdate()
- At this point, I successfully see two copies of
dynamicItem
on screen - On any key, I:
- set
snapshotItem.sourceItem
to an empty, dummy Item, to make the next step less likely to cause problems - destroy
dynamicItem
问题是,当按下一个键时,两个副本都会从屏幕上消失,而我希望
snapshotItem
保留其中一个.The problem is that when a key is pressed, both copies disappear from screen, when I want the
snapshotItem
one to remain.注意:如果您对实现这一目标的动机感兴趣,请参阅我之前的问题一>.
Note: If you're interested in the motivation behind wanting to achieve this, see 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
本身.You don't need to use a
dummyItem
. You can set thesourceItem
to theShaderEffectSource
itself.也许你应该将
recursive
设置为true
,但没有它也能工作.Maybe you should set
recursive
totrue
, but it also works without it.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 } } }
这篇关于销毁设置为 ShaderEffectSource 的 sourceItem 的 Item 也会隐藏 ShaderEffectSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- set
- 将