PDA

View Full Version : QTextDocument line number



bunjee
7th June 2008, 12:56
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.

bunjee
7th June 2008, 18:09
Let me rephrase:

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

bunjee
8th June 2008, 01:49
:( ... Nobody ?

roxton
8th June 2008, 21:25
Here it is:


QStringList l = textEdit->toPlainText().split (QChar::ParagraphSeparator);
int number_of_lines = l.size();

bunjee
8th June 2008, 22:09
Thanks, but, doesn't work in my case, I need lines not paragraphes and those are not visible characters.

roxton
9th June 2008, 11:04
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.

roxton
9th June 2008, 11:33
The another solutions is:


int lines_count = textEdit->toPlainText().count(QChar::ParagraphSeparator);

patrik08
9th June 2008, 12:56
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.com/svn/trunk/GraphicsViewEdit/qtextdocument_parse/qdocxhtml.cpp
http://fop-miniscribus.googlecode.com/svn/trunk/GraphicsViewEdit/qtextdocument_parse/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.com/svn/trunk/MiniScribus_2.0.Setup/a_structure/xmlhighlighter.cpp
http://fop-miniscribus.googlecode.com/svn/trunk/MiniScribus_2.0.Setup/a_structure/xmlhighlighter.h
is a xml text editor widged and display line nr...

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

lsyer
9th June 2008, 15:40
The another solutions is:


int lines_count = textEdit->toPlainText().count(QChar::ParagraphSeparator);

I have tried the following code and yours method,but found it was useless,lines was zero. I donot know why?


QStringList l = textViewer->toPlainText().split (QChar::ParagraphSeparator);
int number_of_lines = l.size();
printf("bn:%d,lines:%d\n",textViewer->document ()->blockCount(),number_of_lines);

roxton
9th June 2008, 18:04
OK, at least that works:


int lines_count = textEdit->toPlainText().count("\n");

bunjee
9th June 2008, 18:25
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.

patrik08
9th June 2008, 19:18
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.php/GraphicsViewEdit+Layer?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...

bunjee
9th June 2008, 20:51
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.

patrik08
10th June 2008, 07:57
QAbstractTextDocumentLayout is friend from QTextDocument and show all info that you need

lsyer
10th June 2008, 13:49
OK, at least that works:


int lines_count = textEdit->toPlainText().count("\n");


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".