Pyqt5 QWebEngineView和QWebEnginePage

时间:2018-03-11 16:29:17

标签: python pyqt pyqt5 qtwebengine

有人可以在PyQt5中解释QWebEngineView和QWebEnginePage的用法。 我想拦截所有请求,从而覆盖属于QWebEnginePage类的acceptNavigationRequest()方法。但我没有使用任何QWebEnginePage对象,而是直接实现QWebEngineView。

我有一个输入字段,我正在使用此方法从该字段加载URL。

def loadURL(self):
    self.load(QUrl(self.URL))
    print('Loading ', self.URL)

但是加载页面后面的链接是我需要单独处理的。我该怎么做。

1 个答案:

答案 0 :(得分:0)

您需要创建QWebEnginePage的子目录,以重新实现acceptNavigationRequest

class WebEnginePage(QWebEnginePage):

  def acceptNavigationRequest(self, qUrl, requestType, isMainFrame):
    # return True to allow navigation, False to block it
    return True

现在,在您的QWebEngineView中,使用setPage将新课程分配为页面:

class WebEngineView(QWebEngineView):

  def __init__(self, parent = None):
    super().__init__(parent)
    self.setPage(WebEnginePage(self))
相关问题