前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >QML自定义滚动选择条

QML自定义滚动选择条

作者头像
Qt君
发布2019-07-15 15:14:27
1.1K0
发布2019-07-15 15:14:27
举报
文章被收录于专栏:跟Qt君学编程跟Qt君学编程

在PathView控件基础上加入滚动选择条,滚动选择条在这基础上加入Key-Value的做法,key为显示内容,value为实际内容,这样可以避免内容上的转换。

WheelView

使用示例

代码语言:javascript
复制
Row {
    anchors.centerIn: parent
    spacing: 50

    WheelView {
        width: 100; height: 400
        model: [{ display: "0", value: 0 },
            { display: "1", value: 1 },
            { display: "2", value: 2 },
            { display: "3", value: 3 },
            { display: "4", value: 4 },
            { display: "5", value: 5 },
            { display: "6", value: 6 },
            { display: "7", value: 7 },
            { display: "8", value: 8 },
            { display: "9", value: 9 }]
        value: 1

        pathItemCount: 5
        displayFontSize: 70
    }

    WheelView {
        width: 100; height: 400
        model: {
            var list = [];
            for (var i = 0; i < 10; i++)
                list.push({ display: i, value: i });
            return list;
        }
        value: 1

        pathItemCount: 5
        displayFontSize: 70
    }
}

源码

代码语言:javascript
复制
import QtQuick 2.0

PathView {
    id: root

    property variant value
    property int displayFontSize: width/4
    /* (displayStep > 0 && displayStep < 1) Sliding font size recursively reduced.
     * (displayStep == 1) Sliding fonts are equal in size.
     * (displayStep > 1) Sliding font size recursively increased.
     */
    property real displayStep: 0.5 // displayStep > 0

    width: 100; height: 300
    /* Example:
     * note: model Format
     * model: [{ display: "A", value: "1" },
     *         { display: "B", value: "2" },
     *         { display: "C", value: "3" }]
     * value: "1"
     */
    clip: true
    pathItemCount: 7
    preferredHighlightBegin: 0.5
    preferredHighlightEnd: 0.5
    dragMargin: root.width/2
    Component.onCompleted: findCurrentIndex()

    onMovementEnded: value = (model[currentIndex].value)
    onValueChanged: findCurrentIndex()

    Keys.onUpPressed: {
        root.decrementCurrentIndex()
        value = (model[currentIndex].value)
    }

    Keys.onDownPressed: {
        root.incrementCurrentIndex()
        value = (model[currentIndex].value)
    }

    delegate: Item {
        width: root.width
        height: root.height/pathItemCount

        Text {
            anchors.centerIn: parent;
            font.pixelSize: displayFontSize*Number(parent.PathView.textFontPercent);
            text: modelData.display
            color: currentIndex == index ? "#43bfe3" : "#848484"
        }
    }

    path: Path {
        startX: root.width/2; startY: 0

        PathAttribute { name: "textFontPercent"; value: displayStep }

        PathLine { x: root.width/2; y: root.height/2 }

        PathAttribute { name: "textFontPercent"; value: 1}

        PathLine { x: root.width/2; y: root.height }

        PathAttribute { name: "textFontPercent"; value: displayStep }
    }

    function findCurrentIndex() {
        for (var i = 0; i < count; i++)
            if (model[i].value === value)
                currentIndex = i;
    }
}

注意

  • model这里提供了两种生成的方式;
  • 如需要再次定制则在这基础上封装即可。
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-01-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Qt君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用示例
  • 源码
  • 注意
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档