Results 1 to 3 of 3

Thread: count line number in text

  1. #1
    Join Date
    Jan 2006
    Posts
    6
    Thanks
    1

    Default count line number in text

    Hi all!
    I got a server problem about counting the line number in a text.~~
    a mouse click a text,the position of cursor follow clicking to change a new place.How do I kown where it is.....I mean the line number???????many thanks!!!!!!!

  2. #2
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    3
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: count line number in text

    Please read Qt4 documentation about the QTextCursor class.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: count line number in text

    As far as I know, there is no direct method to get the current line number. You might have to count it by yourself by iterating through the text blocks and comparing them to text cursors text block. Please someone correct me if I'm wrong! I hope I am, this isn't very convenient..

    Qt Code:
    1. int line = 0;
    2. QTextBlock cursor = textCursor().block();
    3. for (QTextBlock block = document()->begin(); block != document()->end(); block = block.next())
    4. {
    5. ++line;
    6. if (block == cursor)
    7. break;
    8. }
    9. qDebug() << "line" << line;
    To copy to clipboard, switch view to plain text mode 

    This might become a little costly if it's calculated all the time when cursor moves...
    It might be worth taking a quick look at this article:
    Lazy updating for ascertaining block numbers in a document
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    Alina (23rd August 2006)

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  3. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 09:02
  4. How to get text of last line only in QTextEdit?
    By rajesh in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2006, 13:37
  5. a Text Editor with line numbers...
    By fullmetalcoder in forum Qt Programming
    Replies: 47
    Last Post: 5th April 2006, 11:10

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.