Results 1 to 2 of 2

Thread: Getting start index and length of a line in QTextDocument

  1. #1
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Getting start index and length of a line in QTextDocument

    I am wondering what the correct way is to achieve the following: I would like to have the character indices of the start of a line and its length.
    At the moment I am abusing (again) the textcursor for that:

    m_pTextCursor is a QTextCursor

    Qt Code:
    1. void CQtTextLayout::GetLayoutIndicesOfLine( int line, int *start, int *length )
    2. {
    3. if( m_pTextCursor )
    4. {
    5. m_pTextCursor->movePosition( QTextCursor::Start );
    6. for( int i=0; i<line; i++)
    7. {
    8. m_pTextCursor->movePosition( QTextCursor::Down );
    9. }
    10. m_pTextCursor->movePosition( QTextCursor::StartOfLine);
    11. *start = m_pTextCursor->position();
    12. m_pTextCursor->movePosition( QTextCursor::EndOfLine);
    13. *length = m_pTextCursor->position() - *start;
    14. DebugLog( "QT GetLayoutIndicesOfLine %d %d/%d", line, *start, *length );
    15. }
    16. else
    17. {
    18. *start = 0;
    19. *length = 0;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting start index and length of a line in QTextDocument

    When you ignore line wrapping then each line is a QTextBlock. Have a look at its position and length methods.

    Use the textdocuments findBlock .. functions to access those.

    Hope, thats what you are looking for

    Johannes

Similar Threads

  1. QTextDocument - line spacing
    By mazurekwrc in forum Qt Programming
    Replies: 5
    Last Post: 24th February 2011, 23:23
  2. Key sequence to start new line in editor
    By dwarnold45 in forum Newbie
    Replies: 1
    Last Post: 31st March 2010, 08:25
  3. QGraphicsPolygonItem Returning line to start
    By Terabyte in forum Qt Programming
    Replies: 1
    Last Post: 29th December 2008, 07:22
  4. QTextDocument and line height
    By vooft in forum Qt Programming
    Replies: 0
    Last Post: 25th November 2008, 16:47
  5. QTextDocument line number
    By bunjee in forum Qt Programming
    Replies: 14
    Last Post: 10th June 2008, 13:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.