【发布时间】:2020-02-18 16:41:27
【问题描述】:
根据the Qt docs我应该可以打电话了:
项目替换(目标、项目、属性、操作)
用指定的项目替换堆栈上的一个或多个项目,并且 操作,并且可以选择在项目上应用一组属性。这 item 可以是 Item、Component 或 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
}
}
【问题讨论】: