Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I find the first and (more important) the last QTextBlock in a selection provided by QTextCursor?
Qt 6.11 is out! See what's new in the release blog

How do I find the first and (more important) the last QTextBlock in a selection provided by QTextCursor?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.0k 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.
  • B Offline
    B Offline
    bmanc
    wrote on last edited by
    #1

    I have a QTextEdit, and in it I want to determine the first and last selected block in the selection associated with the cursor returned by QTextEdit::textCursor().

    So far I don't see anything in the API that lets me do this.

    Thanks.

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

      @
      QTextCursor cursor = textEdit->textCursor();
      qDebug() << "your selection : " << cursor.selectionStart() << "~" << cursor.selectionEnd();
      @

      Hope this helps.

      joonhwan at gmail dot com

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bmanc
        wrote on last edited by
        #3

        Thanks for the reply. -Unfortunately I'm not sure that helps what I'm aiming. Sorry for not going into depth about the reason earlier.-

        I'm actually trying to get the QTextBlock. The reason is for bulleted list management. I want to determine the text blocks that are covered by the user's selection. So if they have parts of a bulleted lists selected (could be across different levels of indentation) I can increment/decrement the indentation of each selected block without affecting the non-selected portions of the list.

        Update
        Your suggestion worked for me. Using the selectionStart() and selectionEnd() methods you pointed out along with QTextDocument::findBlock(pos) I got what I needed.
        Thanks

        @
        QTextCursor c = textCursor();
        QTextBlock b1 = document()->findBlock(c.selectionStart());
        QTextBlock b2 = document()->findBlock(c.selectionEnd());
        @

        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