Giorgio Gelardi
Giorgio Gelardi

Reputation: 1008

how to scroll a page inside a qwebview?

I guess this should be a simple task: change a QWebView's content (it always contains several pages of stuff) then scroll the page back in the previous position:

y = self.webView.page().mainFrame().scrollPosition().y()
self.webView.setHtml(looong_html_text)
if y != 0:
    self.webView.scroll(0, y)
    self.webView.page().mainFrame().scroll(0, y)
    self.webView.page().mainFrame().setScrollPosition(QPoint(0, y))
    print(self.webView.page().mainFrame().scrollPosition().y())

But the 3 commands inside the if are completely useless: the page scrolls back to top. What's wrong?

Upvotes: 5

Views: 4632

Answers (2)

Michika Iranga Perera
Michika Iranga Perera

Reputation: 1606

use this code

     ui->webView->page()->mainFrame()->scroll(ui->webView->page()->mainFrame()->contentsSize().width(), ui->webView->page()->mainFrame()->contentsSize().height());

Upvotes: 0

Roberto Alsina
Roberto Alsina

Reputation: 800

This is a bit of a guess:

setScrollPosition will not let you scroll beyond the end of the page.

Since you are scrolling before the page is displayed, the page's effective height is 0 at the time. You could verify this by checking self.webView.page().mainFrame().contentSize()

Maybe you should do the scrolling when contentSizeChanged is triggered.

Upvotes: 1

Related Questions