python - 使用 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_())
解决方案
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"))
推荐阅读
- java - 如何在 Java 中将控制台数据转换为文本文件
- c# - 为什么我会收到“使用未分配的局部变量”错误?
- c# - 虚方法的异步等待
- c - 警告:命令替换:输入中忽略的空字节。如何让 gdb 不忽略输入中的空字节?
- unity3d - 使用 Physics.Raycast 检查是否接地的问题
- c# - Visual Studio 中的圈复杂度
- mysql - phpMyAdmin 设置不混淆变音符号
- cocoa - Cocoa And Swift - 我想使用 Swift 4 和 Cocoa for Mac OS 应用程序进行简单的 rest API 调用
- ruby-on-rails - Rails sass 导入 css 文件无法暂存
- ios - 在 Swift 中使用 Alamofire 调用的额外参数“方法”