PDA

View Full Version : how can I find number of lines in QTextedit ?



newplayer
29th July 2008, 08:37
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:


for(int i=0; i<textedit.lines(); i++)
{

}


and:


for(int i=0; i<textedit.count(); i++)
{

}


but it doesn't work :(

jpn
29th July 2008, 10:38
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.

newplayer
29th July 2008, 11:06
for(int i=0; i<textedit.blockCount(); i++)
{
}


it also doesn't work

jpn
29th July 2008, 11:16
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.

newplayer
29th July 2008, 11:32
blockCount : const int
This property holds the number of text blocks in the document.
Access functions:
int blockCount () const


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.

patrik08
29th July 2008, 14:16
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:





QLineF ScribePage::BlinkCursorLine()
{
QLineF CursorDrawLine(QPointF(0,0),QPointF(0,10)); /* error line */
/* QTextDocument *_d; */
const QRectF xxtmp = _d->documentLayout()->blockBoundingRect(textCursor().block());
QTextFrame *Tframe = _d->rootFrame();
root_format = Tframe->frameFormat();
QTextLine TTline = currentTextLine(textCursor());
if ( TTline.isValid() ) {
int pos = textCursor().position() - textCursor().block().position();
const qreal TextCursorStartTop = TTline.lineNumber() * TTline.height() + xxtmp.top();
const qreal TextCursorStartLeft = TTline.cursorToX(pos) + root_format.leftMargin() + root_format.padding();
CursorDrawLine = QLineF(QPointF(TextCursorStartLeft,TextCursorStart Top),
QPointF(TextCursorStartLeft,TextCursorStartTop + TTline.height()));
}
return CursorDrawLine;
}

QTextLine ScribePage::currentTextLine(const QTextCursor &cursor)
{
const QTextBlock block = cursor.block();
if (!block.isValid())
return QTextLine();

const QTextLayout *LayoutBlock = block.layout();
if (!layout)
return QTextLine();
const int relativePos = cursor.position() - block.position();
return LayoutBlock->lineForTextPosition(relativePos);
}

newplayer
29th July 2008, 14:54
So mayby is other control in which I can check how many lines somebody wrote ?

jpn
29th July 2008, 15:02
'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().

newplayer
29th July 2008, 15:21
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 ?

redkite
13th August 2008, 20:05
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. :rolleyes:


QTextDocument * QTextEdit::document () const

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 :

blockCount : const int

Finishing with a little example (very little) :D :


...
QTextEdit *textEdit;
int i = textEdit->document()->blockCount();
...

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.

wysota
14th August 2008, 23:02
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...