PathView的用法

pathView的使用类似与ListView,都需要模型(model)和代理(delegate),只不过pathView多了一个路径(path)属性,顾名思义路径就是item滑动的路径.
区别与listView的是pathView可以分别设置item的属性不想listView一样每个item的样子都差不多.上个图就能很清晰地看到区别了,下面图片中item绕着z轴旋转了所以字体变卷了
示例图
下面给出一个简单的上下滑动的示例.

import QtQuick 2.0

Rectangle {
    id: root
    width: 300;
    height: 200;
    color: "black";

    ListModel {
        id: imageModel
        ListElement {
            normalSource: "pic/1.png"
            selectedSource: "pic/11.gif"
        }
        ListElement {
            normalSource: "pic/2.png"
            selectedSource: "pic/22.gif"
        }
        ListElement {
            normalSource: "pic/3.png"
            selectedSource: "pic/33.gif"
        }
        ListElement {
            normalSource: "pic/4.png"
            selectedSource: "pic/44.gif"
        }
        ListElement {
            normalSource: "pic/5.png"
            selectedSource: "pic/55.gif"
        }
        ListElement {
            normalSource: "pic/6.png"
            selectedSource: "pic/66.gif"
        }
        ListElement {
            normalSource: "pic/7.png"
            selectedSource: "pic/77.gif"
        }
    }

    Component {
        id: rectDelegate;
        Item {
            id: wrar
            scale: PathView.t_scale
            z: PathView.itemZ
            Image {
                id: icon
                source: pathView.currentIndex===index? normalSource : selectedSource
            }
            implicitWidth: icon.width
            implicitHeight: icon.height
        }
    }

    PathView {
        id: pathView;
        anchors.fill: parent

        pathItemCount: 3;
        preferredHighlightBegin: 0.5;
        preferredHighlightEnd: 0.5;
        highlightRangeMode: PathView.StrictlyEnforceRange;


        maximumFlickVelocity: 200

        delegate: rectDelegate;
        model: imageModel;

        path: pathVertical

        Path {
            id:pathVertical
            property int height: root.height
            startX: width/2
            startY: 0
            PathAttribute { name: "t_scale"; value: 0.5}
            PathAttribute { name: "itemZ"; value: 0}
            PathLine { x: pathVertical.startX; y: height*0.5}

            PathAttribute { name: "t_scale"; value: 0.5}
            PathAttribute { name: "itemZ"; value: 1}
            PathLine { x: pathVertical.startX; y: height}

        }

        focus: true
        Keys.onLeftPressed: decrementCurrentIndex();
        Keys.onRightPressed: incrementCurrentIndex();
    }
}

简单解析一下代码,属性pathItemCount控制显示在界面的item个数(Component中的item),
preferredHighlightBegin: 0.5;
preferredHighlightEnd: 0.5;
highlightRangeMode: PathView.StrictlyEnforceRange;
这几句的作用是pathView进行拖动时当前item一直在中间位置
属性maximumFlickVelocity控制滑动速度,数值越大滑动越快

重点来了,PathLine是一个路径元素
startX: width/2; startY: 0
PathLine { x: pathVertical.startX; y: height*0.5}
PathLine { x: pathVertical.startX; y: height}
这几句代码让pathView排列在水平中心位置,夹在相邻两个PathLine元素中间的PathAttribute代表的是两个PathLine元素间隔路径上item的属性,可以通过自定义PathAttribute控制item的属性

PathAttribute 定义一些属性,它的声明语法类似下面:
PathAttribute { name: “t_scale”; value: 0.5; }
name 属性指定待定义属性的名字, real 类型的 value 属性的值为待定义的属性的值。
PathAttribute 放在某个路径段的前面,指明这段路径起始时的属性值;路径段后面的 PathAttribute 指明路径段终止时的属性值;而在路径段上的属性值, Path 会根据起、止值自动插值计算。
代码运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值