0

我想在我的 pyside 应用程序中支持自定义协议,但没有成功。到目前为止,我尝试过:

class MainWindow(QWebView):
    def __init__(self, parent=None):
        oldManager = self.page().networkAccessManager()
        self.page().setNetworkAccessManager(NetworkAccessManager(self, oldManager))

#in another file
class NetworkAccessManager(QNetworkAccessManager):
    def __init__(self, parent, oldManager):
        QNetworkAccessManager.__init__(self)
        self.oldManager = oldManager
        self.setCache(oldManager.cache())
        self.setCookieJar(oldManager.cookieJar())
        self.setProxy(oldManager.proxy())
        self.setProxyFactory(oldManager.proxyFactory())
        print('There')

    def createRequest(self, operation, request, data):
        print('And there')

这导致在segmentation fault窗口下。我看到了这个

目前不支持在 PySide.QtWebKit.QWebPage 使用后更改网络访问管理器。

但我不知道在这种情况下它会在哪里使用。设置网络管理器后尝试设置网页对象,分段错误消失。

PS:控制台内没有显示任何打印语句。

4

1 回答 1

1

如果 createRequest 没有返回回复,它就会崩溃。所以完整的解决方案是:

class MainWindow(QWebView):
    def __init__(self, parent=None):
        oldManager = self.page().networkAccessManager()
        self.setPage(DebugWebPage()) #if you want to set a custom page
        self.page().setNetworkAccessManager(NetworkAccessManager(self))

class NetworkAccessManager(QNetworkAccessManager):
    def __init__(self, parent):
        QNetworkAccessManager.__init__(self)

    def createRequest(self, operation, request, data):
        if request.url().scheme() != 'page':
            return QNetworkAccessManager.createRequest(self, operation, request, data)

        if operation == self.GetOperation:
            # Handle page:// URLs separately by creating custom
            # QNetworkReply objects.
            reply = PageReply(self, request.url(), self.GetOperation)
            print('here')
            return reply
        else:
            return QNetworkAccessManager.createRequest(self, operation, request, data)

class PageReply(QNetworkReply):
    def __init__(self, parent, url, operation):
        QNetworkReply.__init__(self, parent)
        self.content = '<html><head><title>Test</title></head><body>This is a test.</body></html>'
        self.offset = 0

        self.setHeader(QNetworkRequest.ContentTypeHeader, 'text/html; charset=utf-8')
        self.setHeader(QNetworkRequest.ContentLengthHeader, len(self.content))
        QTimer.singleShot(0, self, SIGNAL('readyRead()'))
        QTimer.singleShot(0, self, SIGNAL('finished()'))
        self.open(self.ReadOnly | self.Unbuffered)
        self.setUrl(url)

    def abort(self):
        pass

    def bytesAvailable(self):
        return len(self.content) - self.offset + QNetworkReply.bytesAvailable(self)

    def isSequential(self):
        return True

    def readData(self, maxSize):
        if self.offset < len(self.content):
            end = min(self.offset + maxSize, len(self.content))
            data = self.content[self.offset:end]
            self.offset = end
            return data

注意:我真的不知道为什么,但是脚本在网络管理器中时出现的任何错误或回复都会导致分段错误。

在此基础上进行一些修正。

于 2013-01-31T20:27:37.890 回答