Results 1 to 15 of 15

Thread: QTextDocument line number

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QTextDocument line number

    Hey there,

    I want to know the number of line my QTextDocument is splitted into, any way to do that ?
    I'm not sure "Blocks" are the same thing.

    Thanks.

  2. #2
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument line number

    Let me rephrase:

    Is there a way to know wether a QTextDocument is taking one line OR broke into several lines ?

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument line number

    ... Nobody ?

  4. #4
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument line number

    Here it is:
    Qt Code:
    1. QStringList l = textEdit->toPlainText().split (QChar::ParagraphSeparator);
    2. int number_of_lines = l.size();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument line number

    Thanks, but, doesn't work in my case, I need lines not paragraphes and those are not visible characters.

  6. #6
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument line number

    In QTextDocument, the paragraph is the single line (not wrapped). Paragraphs are the lines that internally separated by QChar::ParagraphSeparator instead of \n. So getting the number of paragraphs, you'll get the count of lines.

  7. #7
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument line number

    The another solutions is:
    Qt Code:
    1. int lines_count = textEdit->toPlainText().count(QChar::ParagraphSeparator);
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: QTextDocument line number

    Quote Originally Posted by bunjee View Post
    Hey there,

    I want to know the number of line my QTextDocument is splitted into, any way to do that ?
    I'm not sure "Blocks" are the same thing.

    Thanks.
    1- QTextDocument if its save to html it save it one only 1 line html and 2-3 css line.
    2- if you like to split QTextBlock to count and check text ... you can iterate like source from
    http://fop-miniscribus.googlecode.co.../qdocxhtml.cpp
    http://fop-miniscribus.googlecode.co...se/qdocxhtml.h is a QTextDocument to html without mountains from css chunk...


    3- if you like to drawline nr. by editing from viewport have a look on source:
    http://fop-miniscribus.googlecode.co...ighlighter.cpp
    http://fop-miniscribus.googlecode.co...lhighlighter.h
    is a xml text editor widged and display line nr...

    if not 1,2,3 please What Is It your target?

  9. #9
    Join Date
    May 2007
    Posts
    2

    Default Re: QTextDocument line number

    Quote Originally Posted by roxton View Post
    The another solutions is:
    Qt Code:
    1. int lines_count = textEdit->toPlainText().count(QChar::ParagraphSeparator);
    To copy to clipboard, switch view to plain text mode 
    I have tried the following code and yours method,but found it was useless,lines was zero. I donot know why?
    Qt Code:
    1. QStringList l = textViewer->toPlainText().split (QChar::ParagraphSeparator);
    2. int number_of_lines = l.size();
    3. printf("bn:%d,lines:%d\n",textViewer->document ()->blockCount(),number_of_lines);
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument line number

    OK, at least that works:
    Qt Code:
    1. int lines_count = textEdit->toPlainText().count("\n");
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument line number

    Hey there,

    let me explain what I really want.

    - I have a QTextDocument.
    - I setHtml with some html content.
    - I call QTextDocument::setTextWidth to set the page width.

    -> The text is sliced into several lines to fit the Page width.

    >> I want to know the NUMBER of those lines.

    (not the number of lines in a QString).

    Thanks.

  12. #12
    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: QTextDocument line number

    what is parent from QTextDocument ?
    1- QGraphicsTextItem , 2 - QTextEdit or 3 - QTextBrowser
    QTextDocument can only stay on this 3 parent by edit ... i not know other.
    Only GraphicsViewEdit ( http://www.qt-apps.org/content/show....?content=80234 ) having other special small Api to handle QTextDocument.

    this X parent tell you how may line you having...

    and QAbstractTextDocumentLayout tell you all corect rect from each line and size..
    search QTextLine on this forum.... and you can grab the exact QTextcursor x,y .

    QTextDocument ::setPageSize ( const QSizeF & size ) break line and on draw level
    QAbstractTextDocumentLayout is paint the correct size...

  13. #13
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument line number

    I'm sorry but :

    - if I setTextWidth on a document.
    -> The page size changes BEFORE the paint event.

    So the document has been laid out, and the page size calculated.

    There should be a WAY of obtaining this line break number.

    The problem is the fact that some component are missing in QTextDocument.

  14. #14
    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: QTextDocument line number

    QAbstractTextDocumentLayout is friend from QTextDocument and show all info that you need

  15. #15
    Join Date
    May 2007
    Posts
    2

    Default Re: QTextDocument line number

    Quote Originally Posted by roxton View Post
    OK, at least that works:
    Qt Code:
    1. int lines_count = textEdit->toPlainText().count("\n");
    To copy to clipboard, switch view to plain text mode 
    Sorry, but the above code is no use etheir,count("\n") just same as textViewer->document ()->blockCount(),is the actual lines in the file breaked by the key "Enter".

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  2. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  3. Line Number - QTextEdit...???
    By deepusrp in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 16:34
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.