0

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
    As the documentation will tell you, QIODevice is an abstract class, i.e., you are supposed to subclass it and implement the various methods yourself, rather than directly instantiating it. – mdurant Oct 29 '14 at 13:37

2 Answers2

0

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.

shevron
  • 3,463
  • 2
  • 23
  • 35
  • ba = QByteArray() buff = QBuffer(ba) buff.open(QIODevice.WriteOnly) print buff printing is: `` not`` – user3446100 Oct 29 '14 at 13:56
  • sorry, i'm update my question, ok in ba but this device not work in `createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData)` – user3446100 Oct 29 '14 at 14:36
  • 1
    What do you mean by "not work"? Are you getting an error message? BTW would help if you sitck to Python code examples (not C++/Python mix); also there is no need to paste the class `__repr__` string - it would be much more readable if you just wrote `QBuffer` or `PyQt4.QtCore.QBuffer` instead of those horrible `` strings. – shevron Oct 29 '14 at 21:44
  • I'm again update my question! After call method - `createRequest(manager, operation, request, data)` with **QBuffer** in **data** script hangs up... – user3446100 Oct 29 '14 at 22:19
0

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

andrean
  • 6,717
  • 2
  • 36
  • 43