在“StackView具体解释(1)”中,我们学习了StackView的基本使用方法,这次呢,我们来讲delegate的定制、被管理的View的生命周期、查找View等主题。

本文还会用到“StackView具体解释(1)”中的演示样例,如有须要能够回头看看。

附加属性

首先看看StackView提供的附加属性 Stack(后面会用到):

  • Stack.index,index代表当前Item在StackView里的索引,从0開始哦,和StackView.depth不同哦,depth从1開始。
  • Stack.status。这是一个枚举值,代表当前Item的状态。

  • Stack.view,指向当前Item所属的StackView实例

我们能够在StackView管理的Item里直接訪问附加属性。后面会有演示样例。

查找View

StackView有一个find方法:find(func, onlySearchLoadedItems)。

这种方法让我们提供一个比較函数,它会对StackView管理的页面应用指定的func函数,当func返回true时,就觉得找到了须要的View,find()会返回这个View。第二个參数onlySearchLoadedItems为true时,说明仅仅查找载入到内存中的Item。为false时,则查找全部Item。

来看一个简单的样例。基于之前的样例改动的。改动的地方我做了标注。

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2 Window {
title: "StackViewDemo";
width: 480;
height: 320;
visible: true; StackView {
id: stack;
anchors.fill: parent; /*
onBusyChanged: {
console.log("busy - " + stack.busy);
}
*/ Text {
text: "Click to create first page";
font.pointSize: 14;
font.bold: true;
color: "blue";
anchors.centerIn: parent; MouseArea {
anchors.fill: parent;
onClicked: if(stack.depth == 0)stack.push(page);
}
}
} Component {
id: page; Rectangle {
color: Qt.rgba(stack.depth*0.1, stack.depth*0.2, stack.depth*0.3);
property alias text: txt.text; //property alias Text {
id: txt; //new code
anchors.centerIn: parent;
font.pointSize: 24;
font.bold: true;
color: stack.depth <= 4 ? Qt.lighter(parent.color) : Qt.darker(parent.color);
//在onCompleted中为text属性赋值
//避免属性绑定,方便查找。
Component.onCompleted: {
text = "depth - " + stack.depth;
}
} Button {
id: next;
anchors.right: parent.right;
anchors.bottom: parent.bottom;
anchors.margins: 8;
text: "Next";
width: 70;
height: 30;
onClicked: {
if(stack.depth < 8) stack.push(page);
}
} Button {
id: back;
anchors.right: next.left;
anchors.top: next.top;
anchors.rightMargin: 8;
text: "Back";
width: 70;
height: 30;
onClicked: {
if(stack.depth > 0) stack.pop();
}
} Button {
id: home;
anchors.right: back.left;
anchors.top: next.top;
anchors.rightMargin: 8;
text: "Home";
width: 70;
height: 30;
onClicked: {
if(stack.depth > 0)stack.pop(stack.initialItem);
}
} Button {
id: clear;
anchors.right: home.left;
anchors.top: next.top;
anchors.rightMargin: 8;
text: "Clear";
width: 70;
height: 30;
onClicked: {
if(stack.depth > 0)stack.clear();
}
} //new code
Button {
id: popTo3;
anchors.right: clear.left;
anchors.top: next.top;
anchors.rightMargin: 8;
text: "PopTo3";
width: 70;
height: 30;
onClicked: {
var resultItem = stack.find(
function(item){
console.log(item.text);
return item.text === "depth - 3" ? true : false;
}
);
if(resultItem !== null)stack.pop(resultItem);
}
}
}
}
}

我给id为page的组件。加入了一个PopTo3的button,点击它,会退栈。直到栈的深度为3。

find()方法查找时的顺序。是从栈顶到栈底。

假设找不到,则返回null。假设调用pop(null),则会直接退到栈底,即栈深度为1。所以我在代码里做了推断,假设find返回null,就不调用pop。

代码里另一些变化,为了方便通过文本查找,我给page的Rectangle加入了一个属性别名。指向它内部的Text对象的text属性。

事实上假设使用StackView的get(index)方法,PopTo3的onClicked信号处理器还能够重写成以下的样子:

onClicked: {
var resultItem = stack.get(2);
if(resultItem !== null)
posted @
2017-07-10 15:52 
yangykaifa 
阅读(...) 
评论(...) 
编辑 
收藏

最新文章

  1. 如何寻找&ldquo;真爱&rdquo;型合伙人
  2. Golang Import使用入门
  3. 解决Linq第一次调用存储过程时速度慢的问题
  4. Structs 原理图
  5. Java中反射与常用方法
  6. android 下 利用webview实现浏览器功能
  7. iOS UI特效
  8. 网易互联网&amp;网易游戏产品经理面试经验
  9. pytorch错误:Missing key(s) in state_dict、Unexpected key(s) in state_dict解决
  10. vue-cli全局安装
  11. opencv基础教程
  12. 浏览器与服务端请求响应流程与HTTP协议
  13. 多线程-Callable、Future、FutureTask
  14. C#设计模式之10:状态模式
  15. CodeFroces-- Feel Good
  16. CRC-16的原理和实现
  17. Convert PIL Image to byte array?
  18. 《C++标准程序库》笔记之四
  19. POJ 2117 Electricity(割点求连通分量)
  20. 分享一个oracle 完整备份的批处理文件

热门文章

  1. CE工具里自带的学习工具--第四关
  2. viewstate 与 session 区别
  3. python circle nested
  4. 六、面向切面的spring(2)
  5. pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法
  6. Sublime Text3 学习笔记
  7. struts2与常用表格ajax操作的json传值问题
  8. 使用maven的mybatis-generator代码生成器插件生成实体类、mapper配置文件和mapper接口(使用idea)
  9. wepy 编译警告去除办法
  10. 去面试Python工程师,这几个基础问题一定要能回答,Python面试题No4