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. Select whole line on click in TextArea
QtWS25 Last Chance

Select whole line on click in TextArea

Scheduled Pinned Locked Moved Solved QML and Qt Quick
textarea
8 Posts 3 Posters 3.5k 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.
  • T Offline
    T Offline
    tyrathect
    wrote on last edited by
    #1

    When the user sets the cursor somewhere in the text, I'd like to have TextArea select the whole line. TextArea doesn't provide an easy way to figure out in which line the cursor is, though. There's only a cursorPosition property, which is a single integer, and since lines can have different lengths, it can't be used to figure out the cursor's current line. How can I get the line number the cursor is in?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      johnsmith
      wrote on last edited by
      #2

      There are methods to set the selection: select(), moveCursorSelection(), etc.

      Try to connect the clicked signal to a slot that places the cursor at the start of the current line, and then calls moveCursorSelection to move the cursor at the end of the current line and select everything.

      T 1 Reply Last reply
      0
      • J johnsmith

        There are methods to set the selection: select(), moveCursorSelection(), etc.

        Try to connect the clicked signal to a slot that places the cursor at the start of the current line, and then calls moveCursorSelection to move the cursor at the end of the current line and select everything.

        T Offline
        T Offline
        tyrathect
        wrote on last edited by
        #3

        @johnsmith how do I know which positions are the start and end of the line?

        the_T J 2 Replies Last reply
        0
        • T tyrathect

          @johnsmith how do I know which positions are the start and end of the line?

          the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #4

          @tyrathect
          You should take a look at QTextCursor Doc
          richtext cursor

          -- No support in PM --

          T 1 Reply Last reply
          1
          • T tyrathect

            @johnsmith how do I know which positions are the start and end of the line?

            J Offline
            J Offline
            johnsmith
            wrote on last edited by
            #5

            @tyrathect Actually, forget about that. Any way I can think of is too convoluted. The QTextCursor suggestion is better.

            1 Reply Last reply
            0
            • the_T the_

              @tyrathect
              You should take a look at QTextCursor Doc
              richtext cursor

              T Offline
              T Offline
              tyrathect
              wrote on last edited by
              #6

              @the_ How can I obtain TextArea's QTextCursor? There's no method to access it.

              J 1 Reply Last reply
              0
              • T tyrathect

                @the_ How can I obtain TextArea's QTextCursor? There's no method to access it.

                J Offline
                J Offline
                johnsmith
                wrote on last edited by johnsmith
                #7

                @tyrathect You don't. You access the QTextDocument of the TextArea, and create a cursor to manipulate it, something similar to this:

                var textDocument = myTextArea.textDocument
                QTextCursor cursor(textDocument);
                //have fun with your cursor
                
                T 1 Reply Last reply
                0
                • J johnsmith

                  @tyrathect You don't. You access the QTextDocument of the TextArea, and create a cursor to manipulate it, something similar to this:

                  var textDocument = myTextArea.textDocument
                  QTextCursor cursor(textDocument);
                  //have fun with your cursor
                  
                  T Offline
                  T Offline
                  tyrathect
                  wrote on last edited by
                  #8

                  @johnsmith awesome, it worked. Thanks!

                  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