PDA

View Full Version : Restoring cursor position in QTextEdit



s.toonen
30th May 2008, 13:37
Hi people,

I am trying to save to current cursor position, which i want to restore later on. It seems that restoring does not update the actual cursor. Here is a code snippet:


// in constuctor
pTextCursor = new QTextCursor(this->document());

void CCodeWidgetText::saveCursor()
{
this->cursorPos = this->pTextCursor->position();
}

void CCodeWidgetText::restoreCursor()
{
pTextCursor->setPosition(this->cursorPos, QTextCursor::MoveAnchor);
//this->setTextCursor(*pTextCursor);
}


Before this I tried to save the whole QTextCursor object and later on restore it, but that didn't seem to work either. Can someone help me out?

Thanks in advance

jpn
30th May 2008, 13:56
// pTextCursor = new QTextCursor(this->document()); // forget this

void CCodeWidgetText::saveCursor()
{
this->cursorPos = this->textCursor().position();
}

void CCodeWidgetText::restoreCursor()
{
QTextCursor cursor = this->textCursor();
cursor.setPosition(this->cursorPos, QTextCursor::MoveAnchor);
this->setTextCursor(cursor);
}

s.toonen
30th May 2008, 14:44
Thanks, it works now :)