Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How do you select a character from text in Qt?
Forum Updated to NodeBB v4.3 + New Features

How do you select a character from text in Qt?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nazbaluch
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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).

      (Z(:^

      N 1 Reply Last reply
      2
      • sierdzioS sierdzio

        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).

        N Offline
        N Offline
        nazbaluch
        wrote on last edited by
        #3

        @sierdzio That helps, I wasn't aware that you can return a character from the text. i.e myText.text[index] is what I was looking for, thank you.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          In QML, strings are like JavaScript strings (plus additionally they have arg() function), so both square brackets and charAt() should work.

          (Z(:^

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved