Qt Quick之StackView详解(2)

来源:互联网 发布:能看伪恋的漫画软件 编辑:程序博客网 时间:2024/05/17 06:04

在“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);                }            }        }    }}

我给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);}

更简单了吧。当然如果坚持用find,也可以结合附加属性做更有效率的查找:

    onClicked: {        var resultItem = stack.find(                    function(item){                        console.log(item.text);                        return item.Stack.index == 2;                    }                    );        if(resultItem !== null)stack.pop(resultItem);    }

生命周期

正如我们前面提到的,StackView管理的页面,通常是动态创建的,那这些动态创建的对象,就需要在合适的时候销毁,否则就内存泄露了,就悲剧了。

StackView会接管它维护的页面对象(View)的生命周期。当我们调用pop时,那些出栈的Item,会被销毁掉。这一点可以通过给page组件内的Rectangle对象添加一个Component.onDestruction信号处理器来验证,代码如下:

Component.onDestruction: console.log("destruction, current depth - " + stack.depth);

当你调用pop(item)或clear()时,就会触发Item的destruction。

OK,搞明白了这一点我们就放心了。

另外,一个Item在StackView里,其实有很多状态:

  1. instantiation,实例化
  2. inactive,非活动
  3. activating,正在激活
  4. active,活动
  5. deactivating,正在向非活动状态转变
  6. inactive,非活动
  7. destruction,销毁

非栈顶的Item,都处在inactive状态。栈顶的Item,当StackView可见时处于active状态,当StackView不可见时,栈顶的Item处于inactive状态。

更多详情,请参考Qt的帮助吧。

为了检测一个Item的状态变化,可以处理StackView提供的附加属性status的变化处理器。具体的代码是:

Stack.onStatusChanged: console.log("status of item " + Stack.index + ": " + Stack.status);

如你所见,我们用到了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            }        }    }}

上面的代码实现了一个简单的渐隐过渡。

如你所见,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);        }    }}

为了更好的演示渐隐动画效果,我将PropertyAnimation的duration属性设置为2000毫秒。好啦,效果如下图所示:

StackView demo

好啦,StackView就介绍到这里。Qt帮助里还有一些内容,大家可以仔细阅读,必有所获。


更多Qt Quick文章请参考我的Qt Quick专栏,想系统学些Qt Quick(QML),请阅读《Qt Quick核心编程》。

我开通了微信订阅号“程序视界”,关注即可第一时间看到我的原创文章以及我推荐的精彩文章:

程序视界

5 0