0

要更改 qtwebkit 中的 POST 变量,需要更改或替换 createRequest(...) 中的输出数据。如何创建自己的<PyQt4.QtCore.QIODevice object at 0x03BA...>不是 QFile 或 QByteArray。正是 QIODevice 对象!创建可写设备需要它。或者如何转换<PyQt4.QtCore.QBuffer object at 0x03BA...><PyQt4.QtCore.QIODevice object at 0x03BA...>.
QNetworkAccessManager中最常用的这个设备:
https ://qt.gitorious.org/qt/webkit/source/7647fdaf9a4b526581e02fbd0e87c41a96cbfebb:src/network/access/qnetworkaccessmanager.cpp#L941

QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Operation op,
const QNetworkRequest &req,
QIODevice *outgoingData)
...

更新:调用此方法后:

def createRequest(manager, operation, request, data):
    if data.size() > 0:
        post_body = "q=hello"
        output = QtCore.QByteArray()
        buffer = QtCore.QBuffer(output)
        buffer.open(QtCore.QIODevice.ReadWrite)
        buffer.writeData(post_body)
        data = buffer

    reply = QNetworkAccessManager.createRequest(manager, operation, request, data)
    return reply

脚本挂了……

4

2 回答 2

0

基本上你很接近,我想知道为什么你没有得到分段错误,每次我没有设置新数据对象的父对象时都会发生这种情况:

def createRequest(manager, operation, request, data):
    if data.size() > 0:
        data = QBuffer(QByteArray("q=hello"))
        # data was originally a ReadOnly device as well, keep it that way
        data.open(QIODevice.ReadOnly)

    reply = QNetworkAccessManager.createRequest(manager, operation, request, data)
    # must explicitly set the parent of the newly created data object to this reply object. 
    data.setParent(reply)

    return reply

我在这里写了这个确切的问题:https ://github.com/integricho/path-of-a-pyqter/tree/master/qttut07

于 2014-10-31T08:08:54.273 回答
0

如果我理解您的问题,AQBuffer已经是(抽象,如@mdurant 所述)QIODevice类的实现。例如(我在 PySide 上试过这个,但我相信 PyQt 应该是一样的):

>>> from PySide.QtCore import QIODevice, QBuffer, QByteArray
>>> buff = QBuffer(QByteArray())
>>> isinstance(buff, QIODevice)
True

要在 a 中创建可QIODevice写的内容QByteArray,您可以或多或少地执行以下操作:

ba = QByteArray()
buff = QBuffer(ba)
buff.open(QIODevice.WriteOnly)

您现在可以buff像 a 一样写入QIODevice,然后数据将在ba.

于 2014-10-29T13:46:11.603 回答