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 to know which line the specified position is in the TextArea
Forum Updated to NodeBB v4.3 + New Features

How to know which line the specified position is in the TextArea

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 238 Views
  • 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.
  • S Offline
    S Offline
    senmx
    wrote on last edited by senmx
    #1
    TextArea {
        id: text
        readOnly: true
        width: parent.width - 30
        textFormat: Text.RichText
        renderType: Text.NativeRendering
        text: "hello\nhelloh\nellohxxxellohello"
        selectByMouse: true
        selectionColor: "#7887FF"
        selectedTextColor: "#FFF"
    }
    

    After:

    text.cursorPosition = 20;
    text.moveCursorSelection(23, TextEdit.SelectCharacters);
    

    How to know which line the specified position is in the TextArea?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dannick Stark
      wrote on last edited by
      #2

      You can try to get the whole text from the start to the position of the cursor. Then split it into a list of lines and return the length of the list.

         function currentLine(){
              let list = text.text.substring(0, text.cursorPosition).split(/\n/gm)
              return list.length
         }
      
      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