Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用QDesktopServices.openUrl()打开由QWebEnginePage::createWindow()创建的窗口_Python_Pyqt_Pyqt5_Qwebengineview - Fatal编程技术网

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

Python 使用QDesktopServices.openUrl()打开由QWebEnginePage::createWindow()创建的窗口,python,pyqt,pyqt5,qwebengineview,Python,Pyqt,Pyqt5,Qwebengineview,当JavaScript程序请求在新窗口中打开文档时,将调用QWebEnginePage::createWindow(),以创建新窗口,而我希望(1)使用QDesktopServices.openUrl(url)打开新窗口,并且(2)保持QWebEngineView中的视图不变。我的解决方案不能满足(2),所以有更简单的解决方案吗 from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * f

当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_())

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

类伪造页面(QWebEnginePage):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.urlChanged.connect(self.handle\u url\u changed)
@pyqtSlot(QUrl)
def handle_url_已更改(self,url):
QDesktopServices.openUrl(url)
self.deleteLater()
类别WebEnginePage(QWebEnginePage):
def createWindow(自身,windowType):
返回伪造页面(自我)
类WebEngineView(QWebEngineView):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.webPage=WebEnginePage(self)
self.setPage(self.webPage)
self.webPage.setUrl(QUrl(“https://dict.eudic.net/liju/en/good#TingLiju"))

非常感谢!我认为
handle\u url\u changed
的实现可以简化为
QDesktopServices.openUrl(url);self.deleteLater()