销毁设置为 ShaderEffectSource 的 sourceItem 的项目也会隐藏 ShaderEffectSource
Destroying an Item that was set as the sourceItem for a ShaderEffectSource hides the ShaderEffectSource as well
简而言之,我在做什么:
- 我有一个名为
snapshotItem
的 ShaderEffectSource
项目 live: false
- 动态实例化名为
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
}
}
}
您不需要使用 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
}
}
}
简而言之,我在做什么:
- 我有一个名为
snapshotItem
的ShaderEffectSource
项目live: false
- 动态实例化名为
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
}
}
}
您不需要使用 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
}
}
}