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:
Code:
for(int i=0; i<textedit.lines(); i++)
{
}
and:
Code:
for(int i=0; i<textedit.count(); i++)
{
}
but it doesn't work :(
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.
Re: how can I find number of lines in QTextedit ?
Code:
for(int i=0; i<textedit.blockCount(); i++)
{
}
it also doesn't work
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.
Re: how can I find number of lines in QTextedit ?
Code:
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.
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:
Code:
QLineF ScribePage
::BlinkCursorLine() {
/* QTextDocument *_d; */
const QRectF xxtmp
= _d
->documentLayout
()->blockBoundingRect
(textCursor
().
block());
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,TextCursorStartTop
),
QPointF(TextCursorStartLeft,TextCursorStartTop
+ TTline.
height()));
}
return CursorDrawLine;
}
{
if (!block.isValid())
if (!layout)
const int relativePos = cursor.position() - block.position();
return LayoutBlock->lineForTextPosition(relativePos);
}
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 ?
Re: how can I find number of lines in QTextedit ?
Quote:
Originally Posted by
newplayer
'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().
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 ?
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. :rolleyes:
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 :
Code:
blockCount : const int
Finishing with a little example (very little) :D :
Code:
...
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.
Re: how can I find number of lines in QTextedit ?
Quote:
Originally Posted by
redkite
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...