Results 1 to 3 of 3

Thread: QTextEdit: how to get lines?

  1. #1
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QTextEdit: how to get lines?

    I have a QTextEdit and the user writes some text in it. How can I get the lines exactly the same way as they are displayed on the screen? I have to get every line start and line end - if possible the absolute text position *and* the y position of the line.

    Thanks for any help!
    Claudio

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit: how to get lines?

    Quote Originally Posted by claudio View Post
    I have a QTextEdit and the user writes some text in it. How can I get the lines exactly the same way as they are displayed on the screen? I have to get every line start and line end - if possible the absolute text position *and* the y position of the line.

    Thanks for any help!
    Claudio

    here is a sample to get line ...
    from http://juffed.googlecode.com/svn/trunk/qedit/qtedit.cpp

    my own first projekt
    http://www.qt-apps.org/content/show....?content=59422

    as editor now i use only http://www.riverbankcomputing.co.uk/...illa/index.php having all

    Qt Code:
    1. QString TextEdit::GrepTextLines(int startline, int stopline )
    2. {
    3. QStringList cuttextlist;
    4. cuttextlist.clear();
    5. int xli = 1;
    6. for ( QTextBlock block = document()->begin(); block.isValid(); block = block.next(), xli++) {
    7. if (xli == startline || xli > startline) {
    8. if (xli == stopline || xli < stopline) {
    9. cuttextlist.append(GrepLineFromTxBloc(block));
    10. ////////qDebug() << "### cat line GrepTextLines " << xli;
    11. }
    12. }
    13. }
    14. return cuttextlist.join("\n");
    15. }
    16.  
    17. QString TextEdit::GrepLineFromTxBloc( QTextBlock block )
    18. {
    19. QString linetext = "";
    20. if ( m_tabSpaces ) /* if tabulator key on to indent text ? */
    21. {
    22. int nbSpaces = tabStopWidth() / fontMetrics().width( " " );
    23. for (int i = 0; i<nbSpaces; i++) {
    24. linetext.append( " " );
    25. }
    26. linetext.append(block.text());
    27.  
    28. } else {
    29. linetext.append(block.text());
    30. }
    31.  
    32. return linetext;
    33. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit: how to get lines?

    Yea, great! This is my code for scanning the blocks on a mouse move event:

    Qt Code:
    1. for(QTextBlock block = document()->begin(); block.isValid(); block = block.next()) {
    2. if(block.contains(textpos)) {
    3. int posinblock = textpos - block.position();
    4. if(block.layout()->isValidCursorPosition(posinblock)) {
    5. QTextLine tl = block.layout()->lineForTextPosition(posinblock);
    6. int linestartpos = block.position() + tl.textStart();
    7. emit onMouseMoveTextLineStartPos(linestartpos);
    8. }
    9. break;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help, patrik08!

Similar Threads

  1. Spaces between lines in QTextEdit
    By troorl_ua in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 01:06
  2. 'Annotation' of lines in a QTextEdit
    By caduel in forum Qt Programming
    Replies: 21
    Last Post: 23rd June 2007, 16:29
  3. QTextEdit with custom space between lines
    By Lele in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 09:24
  4. space between lines in QTextEdit
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 19th September 2006, 14:05
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07:03

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.