6

如何将桌面应用程序的资源(html、css、js、字体等文件)提供给 QWebView(在 PyQT5 中)?

我想要的是:

  1. 如果可能,PyQT5 的 Webkit 引擎使用自定义方案(例如说custom://app/jquery.js并返回文件)处理请求的资源。
  2. 如果可能,为该自定义方案处理标准 http 动词(GET、POST、...)。

如果这两个是不可能的(至少不可能以纯 Pythonic 方式):

  • 如何拦截来自嵌入式 Webkit 的请求并根据 url 中的模式(比如一些正则表达式)提供资源(html、css、js (?<controller>[^/])/(?<action>[^/]))?
4

1 回答 1

7

您需要创建自己的 QNetworkAccessManager 子类,它为所需协议返回自定义 QNetworkReply,然后将其设置为 QWebView 页面的网络访问管理器。

本文展示了如何做到这一点的一个很好的例子——应用于 PyQt5,它的外观是这样的:

from PyQt5.QtCore import QUrl, QTimer, QIODevice
from PyQt5.QtWidgets import QApplication
from PyQt5.QtNetwork import (QNetworkAccessManager,
                             QNetworkReply,
                             QNetworkRequest)
from PyQt5.QtWebKitWidgets import QWebView

import sys

class ExampleNetworkAccessManager(QNetworkAccessManager):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

    def createRequest(self, operation, request, device):
        if request.url().scheme() == 'example':
            return ExampleReply(self, operation, request, device)
        return super().createRequest(operation, request, device)


class ExampleReply(QNetworkReply):

    def __init__(self, parent, operation, request, device):
        super().__init__(parent=parent)
        self.setRequest(request)
        self.setOperation(operation)
        self.setUrl(request.url())
        self.bytes_read = 0
        self.content = b''

        # give webkit time to connect to the finished and readyRead signals
        QTimer.singleShot(200, self.load_content)

    def load_content(self):
        if self.operation() == QNetworkAccessManager.PostOperation:
            # handle operations ...
            pass
        # some dummy content for this example
        self.content = b'''<html>
            <h1>hello world!</h1>
            <p>...</p>
            </html>'''
        self.open(QIODevice.ReadOnly | QIODevice.Unbuffered)
        self.setHeader(QNetworkRequest.ContentLengthHeader, len(self.content))
        self.setHeader(QNetworkRequest.ContentTypeHeader, "text/html")
        self.readyRead.emit()
        self.finished.emit()

    def abort(self):
        pass

    def isSequential(self):
        return True

    def bytesAvailable(self):
        ba = len(self.content) - self.bytes_read + super().bytesAvailable()
        return ba

    def readData(self, size):
        if self.bytes_read >= len(self.content):
            return None
        data = self.content[self.bytes_read:self.bytes_read + size]
        self.bytes_read += len(data)
        return data

    def manager(self):
        return self.parent()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    wv = QWebView()
    enam = ExampleNetworkAccessManager()
    wv.page().setNetworkAccessManager(enam)
    wv.show()
    wv.setUrl(QUrl("example://test.html"))
    app.exec()
于 2014-03-23T14:07:45.233 回答