在“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.4import QtQuick.Controls 1.3import QtQuick.Window 2.2Window { 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); } } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
我给id为page的组件,添加了一个PopTo3的按钮,点击它,会退栈,直到栈的深度为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)stack.pop(resultItem);}
- 1
- 2
- 3
- 4
更简单了吧。当然如果坚持用find,也可以结合附加属性做更有效率的查找:
onClicked: { var resultItem = stack.find( function(item){ console.log(item.text); return item.Stack.index == 2; } ); if(resultItem !== null)stack.pop(resultItem); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
生命周期
正如我们前面提到的,StackView管理的页面,通常是动态创建的,那这些动态创建的对象,就需要在合适的时候销毁,否则就内存泄露了,就悲剧了。
StackView会接管它维护的页面对象(View)的生命周期。当我们调用pop时,那些出栈的Item,会被销毁掉。这一点可以通过给page组件内的Rectangle对象添加一个Component.onDestruction信号处理器来验证,代码如下:
Component.onDestruction: console.log("destruction, current depth - " + stack.depth);
- 1
当你调用pop(item)或clear()时,就会触发Item的destruction。
OK,搞明白了这一点我们就放心了。
另外,一个Item在StackView里,其实有很多状态:
- instantiation,实例化
- inactive,非活动
- activating,正在激活
- active,活动
- deactivating,正在向非活动状态转变
- inactive,非活动
- destruction,销毁
非栈顶的Item,都处在inactive状态。栈顶的Item,当StackView可见时处于active状态,当StackView不可见时,栈顶的Item处于inactive状态。
更多详情,请参考Qt的帮助吧。
为了检测一个Item的状态变化,可以处理StackView提供的附加属性status的变化处理器。具体的代码是:
Stack.onStatusChanged: console.log("status of item " + Stack.index + ": " + Stack.status);
- 1
如你所见,我们用到了Stack.status附加属性,它是一个枚举值,可以取下面的值:
- Stack.Inactive: 0,不可见
- Stack.Activating: 2,正在激活
- Stack.Active: 3,活动状态
- Stack.Deactivating: 1,正变为非激活状态
嗯,有了这些值,就可以对照着日志观察一个Item的状态变化了。
定制过渡动画(StackViewDelegate)
StackView的delegate属性可以定制页面切换时的过渡动画(push、pop、replace),它的类型是StackViewDelegate。
StackViewDelegate有下列属性:
- popTransition ,类型为Component,指定出栈过渡。
- pushTransition ,类型为Component,指定入栈过渡。
- replaceTransition ,类型为Component,指定替换操作的过渡。
一般地,pushTransition应该是StackViewTransition的实例,StackViewTransition是ParallelAnimation的子类,有exitItem和enterItem两个属性,我们可以指定两个动画,分别应用到exitItem和enterItem上,当过渡发生时,这两个动画会同时执行。
更多关于过渡和动画的介绍,参考Qt帮助吧,或者阅读《Qt Quick核心编程》一书的第12章,这章对各种动画元素和用法做了非常详尽的介绍。
StackViewDelegate还有两个方法,经常用到的是 transitionFinished(properties) 方法。
好啦,这里我引用Qt帮助里StackView页面中的一个小示例来讲解一下:
StackView { delegate: StackViewDelegate { function transitionFinished(properties) { properties.exitItem.opacity = 1 } pushTransition: StackViewTransition { PropertyAnimation { target: enterItem property: "opacity" from: 0 to: 1 } PropertyAnimation { target: exitItem property: "opacity" from: 1 to: 0 } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
上面的代码实现了一个简单的渐隐过渡。
如你所见,StackView的delegate属性的值是StackViewDelegate的实例。而StackViewDelegate则只初始化了pushTransition,当我们不指定popTransition和replaceTransition时,它们使用pushTransition指定的过渡动画。
pushTransistion指向StackViewTransition的一个实例。这个实例提供了两个PropertyAnimation动画,改编target的opacity属性。enterItem的opacity从0到1变化,效果是渐显。exitItem的opacity从1到0变化,效果是渐隐。为了让exitItem恢复正常,重写了transitionFinished方法,将exitItem的opactiy重新还原为1。
我们参考这个,把渐隐动画添加到我们的示例中,代码如下:
import QtQuick 2.4import QtQuick.Controls 1.3import QtQuick.Window 2.2Window { title: "StackViewDemo"; width: 480; height: 320; visible: true; StackView { id: stack; anchors.fill: parent; onBusyChanged: { console.log("busy - " + stack.busy); } Text { id: clue; 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){ clue.visible = false; stack.push(page); } } } delegate: StackViewDelegate { function transitionFinished(properties) { properties.exitItem.opacity = 1 } pushTransition: StackViewTransition { PropertyAnimation { target: enterItem; property: "opacity"; from: 0; to: 1; duration: 2000; } PropertyAnimation { target: exitItem; property: "opacity"; from: 1; to: 0; duration: 2000; } } } } 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; anchors.centerIn: parent; font.pointSize: 24; font.bold: true; color: stack.depth <= 4 ? Qt.lighter(parent.color) : Qt.darker(parent.color); 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(); clue.visible = true; } } 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.Stack.index == 2; } ); if(resultItem !== null)stack.pop(resultItem); } } Component.onDestruction: console.log("destruction, current depth - " + stack.depth); Stack.onStatusChanged: console.log("status of item " + Stack.index + ": " + Stack.status); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
为了更好的演示渐隐动画效果,我将PropertyAnimation的duration属性设置为2000毫秒。好啦,效果如下图所示:
好啦,StackView就介绍到这里。Qt帮助里还有一些内容,大家可以仔细阅读,必有所获。
更多Qt Quick文章请参考我的Qt Quick专栏,想系统学些Qt Quick(QML),请阅读《Qt Quick核心编程》。
我开通了微信订阅号“程序视界”,关注即可第一时间看到我的原创文章以及我推荐的精彩文章:
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow