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. [SOLVED] Access to QTextEdit lines
QtWS25 Last Chance

[SOLVED] Access to QTextEdit lines

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 8.7k 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.
  • A Offline
    A Offline
    Alexander Borodulya
    wrote on last edited by Alexander Borodulya
    #1

    Good day all!
    I have QTextEdit widget. And I need random access to specified line for this widget.
    I found two ways to resolve this task:

    @
    //1
    QTextEdit * textEdit;
    QTextDocument * doc = textEdit->document();
    QString line = doc->findBlockByLineNumber( 0 ).text();

    //2
    QTextEdit * textEdit;
    QString allText = textEdit->toPlainText();
    int fromIndex = 0;
    int toIndex = 0;
    while ( ( toIndex = allText.indexOf("n", fromIndex) ) != -1 )
    {
    // Load next line from all text
    QString line = QString( allText.begin() + fromIndex, toIndex - fromIndex );
    }
    @

    Who know other different or more correct ways to resolve this task?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      What's wrong with method 1?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alexander Borodulya
        wrote on last edited by
        #3

        Here all right.
        But I do not know what could be inside QTextBlock - one line or multiple lines?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Conceptually, there could be more than one line in a block. However, the documentation for QTextBlock::firstLineNumber states:
          [quote]Unless the layout supports it, the line number is identical to the block number.[/quote]
          So, I guess that in normal circumstances, every line is represented by one block.

          I agree that it is a bit unclear...

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            A QTextBlock basically represents a paragraph (eg. <p> in HTML). That is "one line" in the sense of missing line breaks (<br> in HTML, but you can add them!). It is not one line in the sense of visible lines in QTextEdit, as a paragraph consisting of - say - 1000 chars is wrapped and displayed in multiple lines on the screen.

            http://www.catb.org/~esr/faqs/smart-questions.html

            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