[Qt-interest] Cursor in QWebPage?

Strahinja Markovic strahinja.markovic at gmail.com
Wed Nov 18 00:23:00 CET 2009


Eric Clark wrote:

> Hello All,
> 
> I was wondering if anyone knew of any way to get the position of the text
> cursor in an editable QWebPage? I would prefer the index in the text
> document, but I can probably get that if I knew the point on the screen.
> Maybe there is a way to manipulate a QWebPage or QWebFrame with a
> QTextCursor? If not, can anyone tell me if there is any plan to implement
> something like that?
> 
> Thanks,
> Eric

The best thing I can come up with is to use JavaScript. AFAIK you cannot do 
this with just the Qt API.

For the JavaScript, look at these pages on the Mozilla Developer Center:
https://developer.mozilla.org/en/DOM/selection
https://developer.mozilla.org/en/DOM/range

So you can get the Range object representing the current selection with
"range =  window.getSelection().getRangeAt(0);". If the user hasn't selected 
anything and there is only a blinking cursor (actually called a _caret_), 
you will get a collapsed range (a range with the start and end points in the 
same place).

The collapsed range will only get you the char offset from the beginning of 
the current text node... from there, you can get the offset from the 
beginning of the document through a lot of JavaScript hardship. Make your 
life a bit easier and embed jQuery too.

Just FYI, you're in for a world of hurt. :)

BTW you run JS with this function:
http://doc.trolltech.com/4.5/qwebframe.html#evaluateJavaScript

Sincerely,

Strahinja Markovic




More information about the Qt-interest-old mailing list