How to implement the "reverse" action in QWebView / QWebPage

I have a simple widget that is a QWebView. I load it using setHtml (). In most cases, it’s just that the user can read the stylized text, however there are several links, and if they click on it, QWebView correctly displays the linked page, but now there is no way to return to the original page. I want to implement the back key combination (or, maybe the back button, but the problem is the same). And I cannot figure out how to say my QWebView or its QWebPage for this. Some code tries everything I can think of:

class helpDisplay(QWebView): def __init__(self, parent=None ): super(helpDisplay, self).__init__(parent) self.backAction = self.page().action(QWebPage.Back) self.backAction.setEnabled(True) # initially was False self.backAction.setShortcut(QKeySequence.Back) # was empty, now ctl-[ ... self.setHtml(...) # big string input from a file ... def keyPressEvent(self, event): # trap keys if event.key() == Qt.Key_B: # temporary for testing self.page().triggerAction(QWebPage.Back) self.backAction.activate(QAction.Trigger) 

None of this causes navigation backward through the link. Beat ctl- [does nothing. Pressing b enters the keyPressEvent trap and triggers triggerAction and is activated, but nothing happens.

Edit: found WebPage.history () and added the following to the key-b trap: self.page (). History(). back () This view works: if I click start-> A, self.page().history().canGoBack() is False and self.page().history().back() does nothing. However, if I click another start-> A-> B link, now it can GoBack () and return to page A. But I can’t go back to the original page loaded with setHtml ().

Conclusion: WebView.setHtml () does not create an entry in WebPage.history. This may explain why backAction is not working ...

Further editing: after working in Qt Assistant, I found that under QWebFrame.setHtml () it admits: "Note: this method will not affect the session or global history ..." Unfortunately, they did not carry this QWebPage or QWebView note. Actually, this makes sense: a story item will usually just be a URL, so it’s not too weird that they don’t want to store 20K or 50K html text as a story item.

+4
source share
1 answer

To shorten some of your code, QWebView has a back () slot: http://doc.qt.io/archives/qt-4.7/qwebview.html#back .

As an aside, since you found that using setHtml () does not seem to create a history record, you can try one of these suggestions:

Use your story to see if it can come back. If possible, come back. If it cannot run setHtml again with the source code, since you know that this was the first page.

Or write your pages to the temp html file and instead use the load () method for the file: /// url. This can record history.

+1
source

All Articles