使用 QDesktopServices.openUrl() 打开由 QWebEnginePage::createWindow() 创建的 window

use QDesktopServices.openUrl() to open window created by QWebEnginePage::createWindow()

当 JavaScript 程序请求在新的 window 中打开文档时,将调用 QWebEnginePage::createWindow() 来创建新的 window,而我想 (1) 使用 QDesktopServices.openUrl(url) 打开新的 window,(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_())

一个可能的解决方案是创建一个仅用于获取 url 的页面,当您获取它时,将其删除并使用 QDesktopServices::openUrl() 启动 url:

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"))