[SOLVED] Skipping indices with a repeater

Issue

How do I skip the indices in a repeater such that I can create the following exact string: A • D • G • J • M • P • S • V • Z Is this achievable?

My new code based on suggested answers, I created a function newLetters(index) to determine which letters to print and now my output gives the following string: 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] : " • "
            }
        }
    }
}

Solution

You could use a Loader as delegate.

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

here condition should be a function that returns true for the indices you want to show, and defaultDelegate is the delegate you want to show for for those indices.

Another way is to just hide the text element.

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

Answered By – Timon van der Berg

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *