ariya.io About Talks Articles

Scrolling QWebFrame programmatically

1 min read

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.

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook