QML自定義滾動(dòng)選擇條

在PathView控件基礎(chǔ)上加入滑動(dòng)選擇條,滑動(dòng)選擇條在這基礎(chǔ)上加入Key-Value的做法,key為顯示內(nèi)容,value為實(shí)際內(nèi)容,這樣可以避免內(nèi)容上的轉(zhuǎn)換,方便操作。

WheelView.gif

使用示例

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
    }
}

源碼

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這里提供了兩種生成的方式;
  • 如需要再次定制則在這基礎(chǔ)上封裝即可。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,680評論 1 32
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,377評論 0 17
  • 1 從去年7月張靚穎在演唱會(huì)上主動(dòng)公布戀情而未得到積極回應(yīng),再到去年11月馮軻終于求婚張靚穎; 從幾日前馮軻張靚穎...
    冰糖陳皮閱讀 1,759評論 16 22
  • 根據(jù)目前開源或者比較流行的服務(wù)發(fā)現(xiàn)中間件進(jìn)行比較: 服務(wù)健康檢查 Eureka 使用時(shí)需要顯式配置健康檢查支持;Z...
    詣極閱讀 604評論 0 0
  • 即使不曾刻意想起,但也從未忘記,再見面,有的也只是昨日同窗共讀之情和再續(xù)閨蜜情誼之心,仿佛揮手只在昨日,你我依舊少...
    軒軒j閱讀 315評論 0 0

友情鏈接更多精彩內(nèi)容