views:

44

answers:

1

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?

A: 

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.

Roberto Alsina
I've placed that particular qwebview in a qscrollarea, but what you said seems reasonable, I will check it out soon with another qwebview. Thank you.
Giorgio Gelardi
finally, I tried saving the scroll position before to update the content (self.scrollpos = aWebView.page().mainFrame().scrollPosition()), then restore it in the contentSizeChanged event as suggested (aWebView.page().mainFrame().setScrollPosition(self.scrollpos)). and it works. thank you very much.
Giorgio Gelardi