How to change POST data in qtwebkit?

99封情书 提交于 2019-12-12 04:53:51

问题


For change POST variables in qtwebkit need change or replace outgoingData in createRequest(...). How to create own <PyQt4.QtCore.QIODevice object at 0x03BA...> not QFile or QByteArray. Exactly QIODevice object! It is needed for creation of writable device. Or how to convert <PyQt4.QtCore.QBuffer object at 0x03BA...> to <PyQt4.QtCore.QIODevice object at 0x03BA...>.
This device most used in 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)
...

UPDATE: After call this method:

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

script hangs up...


回答1:


If I'm making sense of your question, A QBuffer is already an implementation of the (abstract, as noted by @mdurant) QIODevice class. So for example (I tried this on PySide but I believe PyQt should be the same):

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

To create a writable QIODevice writing into a QByteArray, you can do more or less the following:

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

You can now write to buff as if it was a QIODevice and then the data will be available in ba.




回答2:


Basically you were close, I wonder why you didn't get a segmentation fault, it happened to me every time when I didn't set the parent object of the new data object:

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

I wrote about this exact issue here: https://github.com/integricho/path-of-a-pyqter/tree/master/qttut07



来源:https://stackoverflow.com/questions/26630961/how-to-change-post-data-in-qtwebkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!