PDA

View Full Version : QTextEdit restore cursor position



artur231
25th May 2008, 14:04
Hi.

I have problem with saving/restoring cursor position after closing/opening my application.
I use textCursor().position() to save position in QSettings and textCursor().setPosition() to restore it, but cursor always is on on position 0. Saving work's ok, if I look in settings file position looks correct.

How to fix this ?

Code (rest of code is based on Qt MDI example) :

void MdiChild::setMdiWindowProperites(_editor_properite s opt)
{
mdiWindowProperites = opt;
setReadOnly(mdiWindowProperites.readOnly);
setFont(QFont(mdiWindowProperites.fontName, mdiWindowProperites.fontSize, QFont::Normal));
//editorOpt.lastDir = opt.lastDir;


if(mdiWindowProperites.syntaxH)
{
if(highlighter <= 0)
highlighter = new Highlighter(document(), mdiWindowProperites.syntaxHColors);
if(highlighter > 0)
highlighter->rehighlight();
}
else
{
if(highlighter > 0)
delete(highlighter);
highlighter = 0;
};

textCursor().setPosition(mdiWindowProperites.curso rPosX);
ensureCursorVisible();
}


I use QT4.4.0 on openSUSE 10.3 KDE3.

Artur

PS.
Sorry for my bad English.

jpn
25th May 2008, 14:11
QTextEdit::textCursor() returns a copy. Try to do it like this:


QTextCursor cursor = textCursor();
cursor.setPosition(mdiWindowProperites.cursorPosX) ;
setCursor(cursor);

artur231
25th May 2008, 14:36
Thanks.
Now works great.


setCursor(cursor);
Must be :
setTextCursor(cursor);


Artur

creatron
15th May 2014, 22:02
Does anyone knows why this solution works?

Have this somewhere in your code..

// save cursor
int cur_position = win.textCursor().position();

One cannot use xxx.setPosition(cur_position) this on the object (QTextEdit) itself, but if one copy it to a local variable, change the local variable, and then write it it back, i can confirm this does work, but don't know why.


/** ---------------------------------------------------------------------------
* @brief display_gui::restore_cursor
* @param win Reference to a QTextEdit object
* @param cur_position The last known curser position
*/
void display_gui::restore_cursor (QTextEdit & win, int cur_position)
{
#ifdef DEBUG_CURSOR_RESTORE
fprintf (stderr,"..... b.NewPos %6d",win.textCursor().position());
#endif

//win.textCursor().setPosition(cur_position, QTextCursor::KeepAnchor); //QTextCursor::MoveAnchor);
//win.textCursor().movePosition(QTextCursor::End, QTextCursor::KeepAnchor); //QTextCursor::MoveAnchor);
// after many many hours .. this seem to be working !!!
// for some UNKNOWN REASON you cannot do this to the original, but you can copy it, change
// what you want , and write it back, then it works
QTextCursor my_cur = win.textCursor();

my_cur.setPosition(cur_position);
win.setTextCursor(my_cur);

#ifdef DEBUG_CURSOR_RESTORE
fprintf (stderr,"..... b.Restore %6d", cur_position);
#endif
}
What is also strange is that I tried to save the cursor in a QTextCursor variable, and restoring it, it does change the cursor position, but then one loose properties like underline and textcolor ??.

Thanks