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 a开发者_如何学运维re completely useless: the page scrolls back to top. What's wrong?
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.
use this code
ui->webView->page()->mainFrame()->scroll(ui->webView->page()->mainFrame()->contentsSize().width(), ui->webView->page()->mainFrame()->contentsSize().height());
精彩评论