How do you select a character from text in Qt?
-
I've uploaded a text file and parsed the content into a text object. Eventually I will be passing each character in the string to a rectangle of its own. I am trying to figure out how to pick a character from the string in qml?
For example:
Text { id:myText text: "The quick brown fox jumps over the lazy dog" visible:false } // And now i would like to transfer the first letter into a rectangle Rectangle{ id: firstRect width:rectText.width height:rectText.height color: "yellow" Text { id:rectText text: // How do I set this as the first character 'T' font.pixelSize: 14 color: "black" } }
So how can set the rectangle with the character from myText to rectText ?
Eventually I will be setting each character in a rectangle of its own. -
Something like this should help:
Text { id:myText text: "The quick brown fox jumps over the lazy dog" visible:false } Repeater { model: myText.text.length Rectangle { Text { text: myText.text[index] } } }
However, for long texts this will likely be quite slow. Depending on what you are actually trying to achieve, using some different methods may be more appropriate (ListView for example, or custom-painted QQuickItem).
-
In QML, strings are like JavaScript strings (plus additionally they have arg() function), so both square brackets and charAt() should work.