qt - Stacklayout 使用 Item Id 而不是 currentIndex = #num 导航
问题描述
我正在使用StackLayout
许多窗口,而不是使用currentIndex = 0
例如我想为项目本身使用 id 的示例。这样我就不需要关心在 中实现的 Items 的顺序是什么StackLayout
,并且代码将更容易阅读。
我没有碰巧找到如何做到这一点,或者它是否真的可能。有任何想法吗?
示例代码:
main.qml
StackLayout {
id: mainStack
width: parent.width - tabbar.width
x: tabbar.width
PageOne {id: pageOne}
PageTwo {id: pageTwo}
PageThree {id: pageThree}
}
Button {
text: "Go to"
onClicked: {
mainStack.currentIndex = 0 // how its done
mainStack.currentIndex = pageOne //what I want: using Item Id
}
}
解决方案
您可以创建一个对 StackLayout 的子项进行搜索的方法,如果找到它,则将 currentIndex 更改为关联的索引:
StackLayout {
id: mainStack
width: parent.width - tabbar.width
x: tabbar.width
function changeTo(item){
for(var i in this.children){
if(this.children[i] === item){
this.currentIndex = i;
return true;
}
}
return false;
}
PageOne {id: pageOne}
PageTwo {id: pageTwo}
PageThree {id: pageThree}
}
Button {
text: "Go to"
onClicked: {
mainStack.changeTo(pageOne)
}
}
推荐阅读
- ios - Create Waves 可视化歌曲 Swift
- reactjs - 是否可以在办公室 UI 结构详细信息列表中包装长标题名称行?
- go - 当非默认是输入通道时,为什么 go 中的选择总是进入默认情况?
- excel - 如何避免数据透视表中的汇总项目
- ios - 如何在 swift 5 xcode 10 中创建用于多个视图的单例视图
- elixir - 在长生不老药中迭代地图并更新值?
- d3.js - 缩放时的 D3js 画笔区域未居中
- python - 基本转换代码的时间和空间复杂度
- server - 如何减少 Angular 6 应用程序在 AWS 上运行的服务器上的加载时间?
- flutter - 新建 Flutter 项目导致项目空白