• Qt Quick之StackView具体解释(2)


    在“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";
         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";
                     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";
                     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";
                     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";
                     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";
                     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)
    
  • 相关阅读:
    Unity 3(一):简介与示例
    MongoDB以Windows Service运行
    动态SQL中变量赋值
    网站发布IIS后堆栈追踪无法获取出错的行号
    GridView Postback后出错Operation is not valid due to the current state of the object.
    Visual Studio 2010 SP1 在线安装后,找到缓存在本地的临时文件以便下次离线安装
    SQL Server 问题之 排序规则(collation)冲突
    IIS 问题集锦
    linux下安装mysql(ubuntu0.16.04.1)
    apt-get update 系列作用
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7146192.html
Copyright © 2020-2023  润新知