PDA

View Full Version : QTextBrowser Grab Last Line Of Output



crisp
10th October 2008, 21:52
First of all, I'm new to these forums so hi to all.

I need to know what options are available to me if I want to grab the last line of input from a QTextBrowser object, so I can do whatever processing I need to do with it.

If you need any more information then please let me know.

Many Thanks.

Boron
11th October 2008, 09:54
Do you mean
1. the very last line of the whole document in the QTextBrowser (the bottom line), or
2. the last line chronological seen?

1. You can move the cursor to the end of document: From there you can select the text until the line start. After that you ask the cursor to return the selected text.

ui.textEdit->moveCursor( QTextCursor::End );
ui.textEdit->moveCursor( QTextCursor::StartOfLine, QTextCursor::KeepAnchor );
QString text = ui.textEdit->cursor().selectedText();
Code is untested, but it should lead the way.

2. Place the cursor to the end of the line you just edited and do the same as above :).

tpf80
11th October 2008, 11:25
depending on how your text browser is filled, you could use:

string = textBrowser->toHtml(); or
string = textBrowser->toPlainText ();

and then use QString::lastIndexOf() to find where the return character that separates the last line from the one before it, then use that index in:
QString::mid ( int position) so that it takes everything from that return character to the end.

crisp
11th October 2008, 13:08
Apologies for the ambiguity there, and many thanks to both of you, that helped a lot.

elcuco
11th October 2008, 13:13
depending on how your text browser is filled, you could use:

string = textBrowser->toHtml(); or
string = textBrowser->toPlainText ();

and then use QString::lastIndexOf() to find where the return character that separates the last line from the one before it, then use that index in:
QString::mid ( int position) so that it takes everything from that return character to the end.

huge performance pennalty! bad! bad! and if the text editor has richtext, the last line... is it? <br>? or </p>? or </div>?

the QTextCursor is the way to go IMHO.