0

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] : " • "
            }
        }
    }
}
test me
  • 47
  • 6
  • Why not just set `letters` to exactly that string and render that instead? – David K. Hess Feb 01 '22 at 14:06
  • setting Text visible: index !== x is not working? – Fausto01 Feb 01 '22 at 14:46
  • I updated my question with new code based on your answer. I'm not sure how to get it exactly like: A • D • G • J • M • P • S • V • Z – test me Feb 01 '22 at 16:18
  • @DavidK.Hess I am trying to use a repeater because I will be adding a MouseArea to each text. I updated my question with new code – test me Feb 01 '22 at 16:36
  • 1
    I believe you have to rethink your algorithm. Unfortunately, I doesn't have any experience with QML. What I imagine is an algorithm like the one I sketched in C++: [Demo on coliru](http://coliru.stacked-crooked.com/a/a4d4fef2e75d6c1a). The trick is to manage the output of `" • "` as a kind of separator instead of the else branch like in your approach. In my C++ sample, I put this into `static` variables. In QML scripting, you might use properties instead (or whatever the scripting language provides to manage some kind of state in a repeater). – Scheff's Cat Feb 02 '22 at 09:25
  • Alternatively, you could extend the else branch of your condition expression with something like `root.letters[index - 1] ? " * " : ""`. (Maybe, introduce yet another function for the 2nd condition.) You have to care about the first iteration, of course. In this case, `index - 1` will be out-of-bound, and this has to be handled as well. – Scheff's Cat Feb 02 '22 at 09:28

1 Answers1

3

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