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

Louis Semprini lsemprini at hotmail.com
Sat Jun 5 07:33:58 CEST 2010


"Esben Mose Hansen" <kde at mosehansen.dk> wrote in message 
news:201006050625.34195.kde at mosehansen.dk...
> On Friday 04 June 2010 19:13:49 Louis Semprini wrote:
>> 2. is there an alternate method rather than saving and restoring
>> QTextCursor  position?
>
> I am no expert in this, but I assume you are using
>
> QTextCursor QTextEdit::textCursor () const
>
> to save the cursor position and
>
> void   setTextCursor ( const QTextCursor & cursor )
>
> to restore it?

No, QTextEdit::textCursor() does not return anything useful, because I am 
using a read-only QTextBrowser.
There is no flashing cursor.

So I have to use cursorForPosition() to determine the user's current 
location within the document.

> So where do cursorForPosition() come into it?
>
> Anyway, cursorForPosition() returns the position for /viewport/ 
> coordinates,
> which means that 0,0 would always be the top of the document. So you 
> probably
> want to translate the 0,0 to viewportcoordinates using something like
> textBrowser->verticalScrollBar()->value() if going by that route.

Good idea, but that's not the problem...

I also noticed the same documentation, however it turns out that "viewport 
coordinates" means the coordinates of the window, not those of the document.

Say I have a document with 1 screenful of paragraphs, and then  1 screenful 
of table.

As I scroll down the first screenful, cursorForPosition(QPoint(0,0)) DOES 
change, and it faithfully reflects the exact cursor position that is at the 
top of the window.  Great!

But as soon as I reach the table, cursorForPosition(QPoint(0,0))  statys the 
same.  It reflects the offset of the start of the table and does not 
increment as I move through the table.  Fail.

If there is text below the table, then again cursorForPosition(QPoint(0,0)) 
starts updating every line.

So clearly there is a bug where cursorForPosition(QPoint(0,0)) does not 
"reach inside" the table, even though it could given Qt's QTextCursor 
architecture.

> But for my money, I'd just store the 2 values for the scrollbars, and the
> value of textCursor(), and the set all thos when restoring. It should 
> work.

I cannot use the scrollbars because when the user starts up again, the 
window/font size may be totally different and so the text may now wrap in a 
totally different way (even 2x or 3x different from before).

Thanks





More information about the Qt-interest-old mailing list