My blog has been moved to ariya.ofilabs.com.

Friday, November 14, 2008

Scrolling QWebFrame programmatically

Here is a challenge: how to scroll QWebFrame programmatically, if the scroll bars are invisible? At first you might think: Aha, let us just set the value of the scroll bars! The idea looks great, however it does not work that way (since QWebView is not a scroll area). Though you can change the value of the scroll bars, they are not reflected in a scrolled view. Because the need for this, in Qt 4.5 there will be functions to get and set the scroll offset, regardless whether the scroll bars are hidden or visible. But then, what about the customers for Qt 4.4?

JavaScript to the rescue. Using the rather infamous hook QWebFrame::evaluateJavaScript, doing the task is rather trivial:

QWebFrame *frame = page()->mainFrame();
frame->evaluateJavaScript(QString("window.scrollTo(%1,%2);").arg(x).arg(y));

For a more complete example, see what I show in our graphics corner lately: WebView and panning support.

2 comments:

Anonymous said...

Artikel di blog ini sangat bagus dan berguna bagi para pembaca. Agar lebih populer, Anda bisa posting berita Anda di lintasberita.com dan memasang WIDGET Lintas Berita di website Anda yang akan berguna bagi semua pembaca di seluruh Indonesia dan menambah incoming traffic di website Anda.

Dragan Ignjatic said...

Is there a way to apply style to the scroll bar in QtWebKit? Is there a more efficient way to code scrolling than evaluating JS code all the time?