根据Qt文档,我应该能够调用:
物品替换(目标、物品、属性、操作(
用指定的项替换堆栈上的一个或多个项,并且操作,并可选地在项上应用一组属性。这个项可以是项、组件或url。返回成为现在的
所以如果我要制定:
stackView.replace(stackView.get(stackView.depth - 3), mycomponent)
我希望位于stackView索引2(小于最大索引(的项替换为mycomponent
。然而,事实并非如此;似乎从堆栈中弹出了索引depth - 1
、depth - 2
和depth - 3
,然后添加了mycomponent
的实例。如何在不丢失较高堆叠对象的情况下替换depth - 3
的索引?
MVP:在下面的代码中,如果我是push
、push
、push
,然后是replace
,我希望Depth at onCompleted: 4
是Current Index: 1
的值。相反,我得到了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
}
}
并非所描述的行为被正确记录(第二段(:
如果指定了目标参数,则该项下的所有项都将被替换。如果target为null,则堆栈中的所有项都将被替换。如果未指定,则仅替换顶部项目。
解决方案可能是首先弹出3个项目(并存储两个顶部项目(,推送您的新组件,最后推送存储的项目:
...
Button {
text: "Replace"
onClicked: {
var item1 = stackView.pop();
var item2 = stackView.pop();
stackView.pop();
stackView.push(mycomponent);
stackView.push(item2);
stackView.push(item1);
}
}
...