如何从内存加载swf文件到QWebEngineView
How to load swf file to QWebEngineView from memory
我正在尝试使用 QWebEngineView 加载 flash (swf) 文件
我想从内存中加载 swf,但到目前为止我只能从 URL
中加载它
data 是 urlopen (urllib) 结果的字节数组。
self.webview
是 QWebEngineView 的一个实例。
self.webview.load(QtCore.QUrl(url)) # works
self.webview.setContent(QtCore.QByteArray(data)) # does not work
使用 URL swf 运行。从 bytearray 加载会导致空页或出现乱码的页面。没有错误显示。
我正在使用 pyqt5 和 python3
您似乎无法直接传递 raw-data,因此解决方法是创建一个自定义 QWebEngineUrlSchemeHandler,它通过 url:
提供资源
from PyQt5 import QtCore, QtWidgets, QtWebEngineCore, QtWebEngineWidgets
class CustomUrlSchemeHandler(QtWebEngineCore.QWebEngineUrlSchemeHandler):
SCHEMENAME = b"custom"
def __init__(self, parent=None):
super().__init__(parent)
self._datas = dict()
def add_data(self, path, mimetype, data):
self._datas[path] = mimetype, data
url = QtCore.QUrl()
url.setScheme(self.SCHEMENAME.decode())
url.setPath(path)
return url
def requestStarted(self, job):
requestMethod = job.requestMethod()
if requestMethod != b"GET":
job.fail(QtWebEngineCore.QWebEngineUrlRequestJob.RequestDenied)
return
requestUrl = job.requestUrl()
requestPath = requestUrl.path()
value = self._datas.get(requestPath)
if value is not None:
mimetype, data = value
buf = QtCore.QBuffer(parent=self)
job.destroyed.connect(buf.deleteLater)
buf.open(QtCore.QIODevice.WriteOnly)
buf.write(data)
buf.close()
job.reply(mimetype.encode(), buf)
else:
job.fail(QtWebEngineCore.QWebEngineUrlRequestJobUrlNotFound)
@classmethod
def registerUrlScheme(cls):
scheme = QtWebEngineCore.QWebEngineUrlScheme(cls.SCHEMENAME)
QtWebEngineCore.QWebEngineUrlScheme.registerScheme(scheme)
if __name__ == "__main__":
import sys
import urllib.request
data = b""
with urllib.request.urlopen(
"https://condor.depaul.edu/sjost/hci430/flash-examples/swf/wheel.swf"
) as response:
data = response.read()
CustomUrlSchemeHandler.registerUrlScheme()
app = QtWidgets.QApplication(sys.argv)
settings = QtWebEngineWidgets.QWebEngineSettings.defaultSettings()
settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
view = QtWebEngineWidgets.QWebEngineView()
handler = CustomUrlSchemeHandler()
view.page().profile().installUrlSchemeHandler(handler.SCHEMENAME, handler)
view.resize(640, 480)
view.show()
url = handler.add_data("test.swg", "application/x-shockwave-flash", data)
view.load(url)
sys.exit(app.exec_())
我正在尝试使用 QWebEngineView 加载 flash (swf) 文件 我想从内存中加载 swf,但到目前为止我只能从 URL
中加载它data 是 urlopen (urllib) 结果的字节数组。
self.webview
是 QWebEngineView 的一个实例。
self.webview.load(QtCore.QUrl(url)) # works
self.webview.setContent(QtCore.QByteArray(data)) # does not work
使用 URL swf 运行。从 bytearray 加载会导致空页或出现乱码的页面。没有错误显示。
我正在使用 pyqt5 和 python3
您似乎无法直接传递 raw-data,因此解决方法是创建一个自定义 QWebEngineUrlSchemeHandler,它通过 url:
提供资源from PyQt5 import QtCore, QtWidgets, QtWebEngineCore, QtWebEngineWidgets
class CustomUrlSchemeHandler(QtWebEngineCore.QWebEngineUrlSchemeHandler):
SCHEMENAME = b"custom"
def __init__(self, parent=None):
super().__init__(parent)
self._datas = dict()
def add_data(self, path, mimetype, data):
self._datas[path] = mimetype, data
url = QtCore.QUrl()
url.setScheme(self.SCHEMENAME.decode())
url.setPath(path)
return url
def requestStarted(self, job):
requestMethod = job.requestMethod()
if requestMethod != b"GET":
job.fail(QtWebEngineCore.QWebEngineUrlRequestJob.RequestDenied)
return
requestUrl = job.requestUrl()
requestPath = requestUrl.path()
value = self._datas.get(requestPath)
if value is not None:
mimetype, data = value
buf = QtCore.QBuffer(parent=self)
job.destroyed.connect(buf.deleteLater)
buf.open(QtCore.QIODevice.WriteOnly)
buf.write(data)
buf.close()
job.reply(mimetype.encode(), buf)
else:
job.fail(QtWebEngineCore.QWebEngineUrlRequestJobUrlNotFound)
@classmethod
def registerUrlScheme(cls):
scheme = QtWebEngineCore.QWebEngineUrlScheme(cls.SCHEMENAME)
QtWebEngineCore.QWebEngineUrlScheme.registerScheme(scheme)
if __name__ == "__main__":
import sys
import urllib.request
data = b""
with urllib.request.urlopen(
"https://condor.depaul.edu/sjost/hci430/flash-examples/swf/wheel.swf"
) as response:
data = response.read()
CustomUrlSchemeHandler.registerUrlScheme()
app = QtWidgets.QApplication(sys.argv)
settings = QtWebEngineWidgets.QWebEngineSettings.defaultSettings()
settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
view = QtWebEngineWidgets.QWebEngineView()
handler = CustomUrlSchemeHandler()
view.page().profile().installUrlSchemeHandler(handler.SCHEMENAME, handler)
view.resize(640, 480)
view.show()
url = handler.add_data("test.swg", "application/x-shockwave-flash", data)
view.load(url)
sys.exit(app.exec_())