[SOLVED] Access to QTextEdit lines



  • 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?



  • What's wrong with method 1?



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.