[英]How to replace() an item in Qml StackView that is not on the top of the stack?
According to the Qt docs I should be able to call:根据Qt 文档,我应该可以调用:
Item replace(target, item, properties, operation)
项目替换(目标、项目、属性、操作)
Replaces one or more items on the stack with the specified item and operation, and optionally applies a set of properties on the item.
用指定的项目和操作替换堆栈上的一个或多个项目,并可选择在项目上应用一组属性。 The item can be an Item, Component, or a url.
项目可以是项目、组件或网址。 Returns the item that became current.
返回成为当前的项目。
So if I were to enact:因此,如果我要制定:
stackView.replace(stackView.get(stackView.depth - 3), mycomponent)
I would expect the item, located at the stackView index 2 less than the largest index, to replace with mycomponent
.我希望位于 stackView 索引 2 小于最大索引的项目替换为
mycomponent
。 However, this is not the case;然而,这种情况并非如此; it seems that index
depth - 1
and depth - 2
and depth - 3
are popped off the stack, then an instance of mycomponent
is added.似乎索引
depth - 1
和depth - 2
和depth - 3
从堆栈中弹出,然后添加了mycomponent
的实例。 How do I replace index of depth - 3
without losing the higher-stacked objects?如何在不丢失堆叠较高的对象的情况下替换
depth - 3
索引depth - 3
?
MVP: In the following code, if I push
, push
, push
, then replace
, I would expect Depth at onCompleted: 4
to be the value for Current Index: 1
. MVP:在下面的代码中,如果我
push
, push
, push
,然后replace
,我会期望Depth at onCompleted: 4
是Current Index: 1
的值。 Instead, I get Depth at onCompleted: 2
相反,我
Depth at onCompleted: 2
获得了Depth at onCompleted: 2
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
visible: true
width: 640
height: 480
id: window
Component {
id: mycomponent
Row {
spacing: 2
Button {
text: "Push"
onClicked: push(mycomponent)
}
Button {
text: "Pop"
onClicked: pop()
}
Button {
text: "Replace"
onClicked: stackView.replace(stackView.get(stackView.depth - 3), mycomponent)
}
Text {
text: "Current Index: " + (stackView.depth - 1)
}
Text {
Component.onCompleted: text = "Depth at onCompleted: " + stackView.depth
}
}
}
StackView {
id: stackView
initialItem: mycomponent
}
}
Not that the described behaviour is correctly documented (second paragraph) :并不是描述的行为被正确记录(第二段) :
If the target argument is specified, all items down to the item will be replaced.
如果指定了目标参数,则将替换该项目的所有项目。 If target is null, all items in the stack will be replaced.
如果 target 为 null,则堆栈中的所有项目都将被替换。 If not specified, only the top item will be replaced.
如果未指定,则仅替换顶部项目。
A solution may be to first pop 3 items (and store the two top items), push your new component and at the end push the stored items:一种解决方案可能是首先弹出 3 个项目(并存储两个顶部项目),推送您的新组件,最后推送存储的项目:
...
Button {
text: "Replace"
onClicked: {
var item1 = stackView.pop();
var item2 = stackView.pop();
stackView.pop();
stackView.push(mycomponent);
stackView.push(item2);
stackView.push(item1);
}
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.