How to change POST data in qtwebkit?

2019-09-07 14:16发布

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...

2条回答
在下西门庆
2楼-- · 2019-09-07 14:20

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.

查看更多
The star\"
3楼-- · 2019-09-07 14:40

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

查看更多
登录 后发表回答