对于更改qtwebkit中的POST变量需要在createRequest(...)中更改或替换outgoingData。
如何创建自己的<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
脚本挂起......
答案 0 :(得分:0)
如果我理解你的问题,A QBuffer
已经是(摘要,如@mdurant)QIODevice
类所述的实现。所以例如(我在PySide上尝试了这个,但我相信PyQt应该是相同的):
>>> from PySide.QtCore import QIODevice, QBuffer, QByteArray
>>> buff = QBuffer(QByteArray())
>>> isinstance(buff, QIODevice)
True
要创建可写QIODevice
写入QByteArray
,您可以执行或多或少的操作:
ba = QByteArray()
buff = QBuffer(ba)
buff.open(QIODevice.WriteOnly)
您现在可以写为buff
,就像它是QIODevice
一样,然后数据将在ba
中提供。
答案 1 :(得分: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