Results 1 to 11 of 11

Thread: how can I find number of lines in QTextedit ?

  1. #1
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default how can I find number of lines in QTextedit ?

    I need make a loop which will be making until end of QTextEdit so I need know hom many lines somebody wrote in QTextEdit.

    I tried:
    Qt Code:
    1. for(int i=0; i<textedit.lines(); i++)
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 

    and:
    Qt Code:
    1. for(int i=0; i<textedit.count(); i++)
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 

    but it doesn't work
    Last edited by jpn; 29th July 2008 at 10:35. Reason: missing [code] tags

  2. #2
    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: how can I find number of lines in QTextedit ?

    There are no such methods as QTextEdit::lines() or QTextEdit::count(). How did you come up with these? There is no point guessing method names, use the great docs.

    See QTextDocument::blockCount.
    J-P Nurmi

  3. #3
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: how can I find number of lines in QTextedit ?

    Qt Code:
    1. for(int i=0; i<textedit.blockCount(); i++)
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 

    it also doesn't work
    Last edited by jpn; 29th July 2008 at 11:11. Reason: missing [code] tags

  4. #4
    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: how can I find number of lines in QTextedit ?

    It's not a method of QTextEdit but QTextDocument. QTextEdit has a method to get the underlying document. Please do me a favor and launch Qt Assistant.

    PS. Please use the '#'-button for code snippets. The 'Qt'-button produces links to the reference documentation, it's not for code formatting.
    J-P Nurmi

  5. #5
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: how can I find number of lines in QTextedit ?

    Qt Code:
    1. blockCount : const int
    2. This property holds the number of text blocks in the document.
    3. Access functions:
    4. int blockCount () const
    To copy to clipboard, switch view to plain text mode 

    this information isn't very useful

    'QTextEdit has a method to get the underlying document.' <--- I don't understand it, I am not from country where English is a state language.

  6. #6
    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: how can I find number of lines in QTextedit ?

    the are not line. only block and sub fragment from block on QTextedit

    at end of one block text break to new line ... but one block can have 10 or more line if no space to display


    open all /4.5.0-src/doc/src/snippets / textdocument* to view source sample.....
    Only from QT version 4.4 >



    To draw on QTextedit you can find each position by iterate block and sub fragment QTextLine

    here a sample to grab Blink cursor:

    Qt Code:
    1. QLineF ScribePage::BlinkCursorLine()
    2. {
    3. QLineF CursorDrawLine(QPointF(0,0),QPointF(0,10)); /* error line */
    4. /* QTextDocument *_d; */
    5. const QRectF xxtmp = _d->documentLayout()->blockBoundingRect(textCursor().block());
    6. QTextFrame *Tframe = _d->rootFrame();
    7. root_format = Tframe->frameFormat();
    8. QTextLine TTline = currentTextLine(textCursor());
    9. if ( TTline.isValid() ) {
    10. int pos = textCursor().position() - textCursor().block().position();
    11. const qreal TextCursorStartTop = TTline.lineNumber() * TTline.height() + xxtmp.top();
    12. const qreal TextCursorStartLeft = TTline.cursorToX(pos) + root_format.leftMargin() + root_format.padding();
    13. CursorDrawLine = QLineF(QPointF(TextCursorStartLeft,TextCursorStartTop),
    14. QPointF(TextCursorStartLeft,TextCursorStartTop + TTline.height()));
    15. }
    16. return CursorDrawLine;
    17. }
    18.  
    19. QTextLine ScribePage::currentTextLine(const QTextCursor &cursor)
    20. {
    21. const QTextBlock block = cursor.block();
    22. if (!block.isValid())
    23. return QTextLine();
    24.  
    25. const QTextLayout *LayoutBlock = block.layout();
    26. if (!layout)
    27. return QTextLine();
    28. const int relativePos = cursor.position() - block.position();
    29. return LayoutBlock->lineForTextPosition(relativePos);
    30. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: how can I find number of lines in QTextedit ?

    So mayby is other control in which I can check how many lines somebody wrote ?

  8. #8
    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: how can I find number of lines in QTextedit ?

    Quote Originally Posted by newplayer View Post
    'QTextEdit has a method to get the underlying document.' <--- I don't understand it, I am not from country where English is a state language.
    You could have just searched QTextEdit docs for 'document'. Well, here it is: QTextEdit::document().
    J-P Nurmi

  9. #9
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: how can I find number of lines in QTextedit ?

    But is other control then QTextEdit in which I can check how many lines somebody wrote ? Because I don't know how I can check it in QTextEdit so mayby is simplier control ?

  10. #10
    Join Date
    Aug 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Wink Re: how can I find number of lines in QTextedit ?

    Hi, Jpn nearly give you the answer, I suggest you to find a tutorial on how using the Qt4 Assistant.

    I'm in a good moon, I will explain you.

    Qt Code:
    1. QTextDocument * QTextEdit::document () const
    To copy to clipboard, switch view to plain text mode 

    As you can see, this method return a QTextDocument* and as jpn said in his first reply, this class have the property you are looking for :
    Qt Code:
    1. blockCount : const int
    To copy to clipboard, switch view to plain text mode 

    Finishing with a little example (very little) :
    Qt Code:
    1. ...
    2. QTextEdit *textEdit;
    3. int i = textEdit->document()->blockCount();
    4. ...
    To copy to clipboard, switch view to plain text mode 
    and i will contain the number of lines that textEdit contain. (more exactly the number of return, I mean an empty line is also counted)

    Hope this will be enough ^^ and hope you will give more importance to the doc !!

    Cheers.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how can I find number of lines in QTextedit ?

    Quote Originally Posted by redkite View Post
    and i will contain the number of lines that textEdit contain. (more exactly the number of return, I mean an empty line is also counted)
    I think these are called paragraphs Of course excluding empty blocks unless you treat them as empty paragraphs...

  12. The following user says thank you to wysota for this useful post:

    WarGamer (4th October 2012)

Similar Threads

  1. problems installing Qt opensource with msvc2008 support
    By odin1985 in forum Installation and Deployment
    Replies: 6
    Last Post: 24th May 2008, 09:06
  2. How to retrieve lines from QTextEdit -- easy way?
    By macias in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 15:44
  3. QTextEdit: how to get lines?
    By claudio in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2007, 21:08
  4. Line Number - QTextEdit...???
    By deepusrp in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 16:34
  5. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

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.