使用中继器跳过索引

Skipping indices with a repeater

我如何跳过转发器中的索引,以便我可以创建以下确切的字符串:A • D • G • J • M • P • S • V • Z这是否可以实现?

我的新代码基于建议的答案,我创建了一个函数 newLetters(index) 来确定要打印哪些字母,现在我的输出给出了以下字符串:A •• D •• G •• J •• M •• P •• S •• V •• Z?

import QtQuick 2.15
import QtQuick.Window 2.15
Window {
    id: root
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    property string letters: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    Rectangle {
        id: control

        anchors.fill: parent
       
    function newLetters(index) {
        if(letters[index] === "A")
            return true
        if(letters[index] === "D")
            return true
        if(letters[index] === "G")
            return true
        if(letters[index] === "J")
            return true
        if(letters[index] === "M")
            return true
        if(letters[index] === "P")
            return true
        if(letters[index] === "S")
            return true
        if(letters[index] === "V")
            return true
        if(letters[index] === "Z")
            return true
    }

        Repeater {
            anchors.fill: parent
            model: 26
            Text {
                text: control.newLetters(index) ? root.letters[index] : " • "
            }
        }
    }
}

您可以使用 Loader 作为委托。

Repeater {
    id: repeater
    model: 26
    Loader {
        sourceComponent: condition(index) ? defaultDelegate : null 
    }
}

此处 condition 应该是一个函数,returns 对于您要显示的索引为真,defaultDelegate 是您要为这些索引显示的委托。

另一种方法是只隐藏文本元素。

Repeater {
    anchors.fill: parent
    model: 26
    Text {
        visible: condition(index)
        text: root.letters[index]
    }
}