View Full Version : QTextEdit Qt4: simple question

17th January 2006, 16:42
Hello all,
I'm filling a QTextEdit with a load of text in on go using
So, here's the silly question ... How do I now go to the end of the text? i.e. scroll all the way down to the last line in the text? Is there a QTextEdit::goToEnd() or whatever?


17th January 2006, 18:43
From QTextEdir docs:

Selection of text is handled by the QTextCursor class, which provides functionality for creating selections, retrieving the text contents or deleting selections. You can retrieve the object that corresponds with the user-visible cursor using the textCursor() method. If you want to set a selection in QTextEdit just create one on a QTextCursor object and then make that cursor the visible cursor using setCursor(). The selection can be copied to the clipboard with copy(), or cut to the clipboard with cut(). The entire text can be selected using selectAll().

bool QTextCursor::movePosition ( MoveOperation op, MoveMode mode = MoveAnchor, int n = 1 )

Moves the cursor in accordance with the MoveOperation op, using MoveMode mode. The move is performed n (default 1) times.

If mode is KeepAnchor, the cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.

QTextCursor::End 11 Move to the end of the document.

17th January 2006, 19:49
Sorry, that's not exactly what I mean. I can move the cursor to the end of the text with ...

QTextCursor cursor = mainView->textCursor();
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
where mainView is my QTextEdit.

This is the problem:
I load some large text into the QTextEdit with ...

QString allTheText;
//...fill the string here...
//put the string into the mainView

Now my QTextEdit has more text in it than can be shown in the visible area. There is a vertical scroll bar on the right with which I can scroll down to the end of the text. However, I'd like my QTextEdit to scroll down for me when I load the text.
See, my texts are log files and when I run my app it should auto-load the newest one and display the most recent data in the file - i.e. the end of the QTextEdit. It would be handy if didn't have to always scroll down through the files.

maybe I'm missing something obvious

17th January 2006, 21:06
I think this will be the most convinient.

void QTextEdit::scrollToAnchor ( const QString & name ) [slot]
Scrolls the text edit so that the anchor with the given name is visible; does nothing if the name is empty, or is already visible, or isn't found.

BTW. It should work using QTextCursor too. Did you try it? Just remember to set the cursor back when you change it.

18th January 2006, 13:03
ok guys, thanks, I've got it. It's a different solution to a different problem though.

This is what I'm doing....

//put the string into the mainView

//move the cursor to the end of the text
QTextCursor cursor(mainView->textCursor());
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
//I have to do this too. - I was forgetting this!!!!

as you guys say ... this should work. And it does ... usually.

However, I had another problem too. I was making my QTextEdit in my mainWindow constructor - and I was loading my file and displaying it from the constructor too. That wasn't moving my cursor.
now I have a

QTimer::singleShot(0, this, SLOT(init()));
and I load my file from init()

That works fine.

thanks for your help