[Qt-interest] QTextEdit/QTextBrowser: how to _really_ save/restore position (cursorForPosition() fails with tables)?

Louis Semprini lsemprini at hotmail.com
Fri Jun 4 19:13:49 CEST 2010


Hello,

I am using a QTextBrowser (which derives from QTextEdit) with QT 4.5.3

When the user quits and re-runs the program, I'd like to bring the user back 
to their previous position in the document.

I need to do it based on cursor location in the document, rather than pixel 
scroll offset, because the user's font or window size may have changed 
between runs.

My documents contain large tables that span multiple screens.  It is 
important to bring the user back to the same table row they were at before. 
I do not have control of the document so I cannot insert tons of <a name>s 
into the document (and even if I did, how would I know which <a name> the 
user is scrolled to on application quit?).

Seems simple enough.

Unfortunately, doing it based on QTextCursor fails for the simple reason 
that this code:

        brws->cursorForPosition(QPoint(0,0)).position();

returns the exact same integer regardless of where QPoint(0,0) falls within 
my large tables -- cursorForPosition always returns the QTextCursor for the 
_beginning_ of the table.

This is strange since the QTextCursor model is very versatile and it should 
be able to address any point within the document, including points inside 
tables.

So the net result is that saving and restoring the QTextCursor position 
works for documents made only from simple paragraphs, but fails for 
documents that include large tables.

So my questions are:

1. is this a known cursorForPosition() bug and is there a fix or workaround?

2. is there an alternate method rather than saving and restoring QTextCursor 
position?

Thanks.





More information about the Qt-interest-old mailing list