首页 > 解决方案 > 使用 QDesktopServices.openUrl() 打开由 QWebEnginePage::createWindow() 创建的窗口

问题描述

当 JavaScript 程序请求在新窗口中打开文档时,QWebEnginePage::createWindow()将调用以创建新窗口,而我想(1)改为使用打开新窗口QDesktopServices.openUrl(url),以及(2)保持 QWebEngineView 中的视图不变。我的解决方案不能满足(2),所以有更简单的解决方案吗?

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWebEngineCore import *
import sys,os


class WebEnginePage(QWebEnginePage): 
  def __init__(self, parent, mdicts=[]):
    super().__init__(parent)
    self.backwardUrl=''

  def acceptNavigationRequest(self, url, navigationType, isMainFrame):  # Navigation requests can be delegated to the Qt application instead of having the HTML handler engine process them by overloading this function. This is necessary when an HTML document is used as part of the user interface, and not to display external data, for example, when displaying a list of results.# The QWebEngineUrlRequestInterceptor class offers further options for intercepting and manipulating requests.
    # print('acceptNavigationRequest-----------------', navigationType, isMainFrame)
    if self.backwardUrl and isMainFrame:
      print('blocked------------',self.backwardUrl)
      self.setUrl(self.backwardUrl)
      QDesktopServices.openUrl(self.backwardUrl)
      self.backwardUrl=''   
      return False 
    return True

  def createWindow(self, windowType):
    print('createWindow')
    self.backwardUrl=self.url()
    return self
    
class WebEngineView(QWebEngineView):
    def __init__(self, parent=None):
        super().__init__(parent)
        # self.mousePressEvent=lambda event:print('mousePressEvent',event.pos())
        # self.mouseMoveEvent=lambda event:print('mouseMoveEvent',event.pos())

        self.webPage = WebEnginePage(self)#self.page()  # QWebEnginePage()

        self.setPage(self.webPage)
        # self.setUrl(QUrl('https://dict.eudic.net/liju/en/good#TingLiju'))

        self.webPage.setUrl(QUrl('https://dict.eudic.net/liju/en/good#TingLiju'))




if __name__ == "__main__":
    app = QApplication(sys.argv)
    webEngineView = WebEngineView()
    webEngineView.show()
    sys.exit(app.exec_())

在此处输入图像描述

标签: pythonpyqtpyqt5qwebengineview

解决方案


A possible solution is to create a page that only serves to obtain the url, and when you get it, delete it and launch the url with QDesktopServices::openUrl():

class FakePage(QWebEnginePage):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.urlChanged.connect(self.handle_url_changed)

    @pyqtSlot(QUrl)
    def handle_url_changed(self, url):
        QDesktopServices.openUrl(url)
        self.deleteLater()


class WebEnginePage(QWebEnginePage):
    def createWindow(self, windowType):
        return FakePage(self)


class WebEngineView(QWebEngineView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.webPage = WebEnginePage(self)
        self.setPage(self.webPage)
        self.webPage.setUrl(QUrl("https://dict.eudic.net/liju/en/good#TingLiju"))

推荐阅读