[PyQt] Subclassing QNetworkReply

Phil Thompson phil at riverbankcomputing.com
Mon Mar 2 17:38:08 GMT 2009


On Mon, 2 Mar 2009 18:33:01 +0100, Detlev Offenbach
<detlev at die-offenbachs.de> wrote:
> On Montag, 2. März 2009, Phil Thompson wrote:
>> On Mon, 2 Mar 2009 18:19:57 +0100, Detlev Offenbach
>>
>> <detlev at die-offenbachs.de> wrote:
>> > Hi,
>> >
>> > I am trying to write a subclass of QNetworkReply. This subclass needs
>> > to
>> > have
>> > it's own variant of the readData protected method. However, it seems,
>>
>> that
>>
>> > this method doesn't work correctly. How do I have to do this
correctly.
>>
>> My
>>
>> > code is shown below.
>> >
>> > --------------
>> > class HelpNetworkReply(QNetworkReply):
>> >     def __init__(self, request, fileData):
>> >         QNetworkReply.__init__(self)
>> >
>> >         self.__data = QBuffer(fileData)
>> >
>> >         self.setRequest(request)
>> >         self.setOpenMode(QIODevice.ReadOnly)
>> >
>> >         self.setHeader(QNetworkRequest.ContentTypeHeader,
>> > 		       QVariant("text/html"))
>> >         self.setHeader(QNetworkRequest.ContentLengthHeader,
>> >                        QVariant(QByteArray.number(fileData.length())))
>> >         QTimer.singleShot(0, self, SIGNAL("metaDataChanged()"))
>> >         QTimer.singleShot(0, self, SIGNAL("readyRead()"))
>> >
>> >     def abort(self):
>> >         # do nothing
>> >         pass
>> >
>> >     def readData(self, buffer, maxlen):
>> >         l = self.__data.readData(buffer, maxlen)
>> >         if self.__data.bytesAvailable() == 0:
>> >             QTimer.singleShot(0, self, SIGNAL("finished()"))
>> >         return l
>> > --------------
>>
>> The Python signature is slightly different as Python strings aren't
>> mutable. There is no buffer argument and you should return the data as a
>> Python string.
>>
>> Phil
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 
> So I have to "return l, buffer" with buffer being a Python string.

No, just buffer. The handwritten code for readData() gets the length from
the buffer.

> Will
> this 
> work even if the method is called from some C++ code of the Qt4 libraries

> (e.g. QWebView or QWebPage)?

Should do - unless there are SIP bugs.

Phil


More information about the PyQt mailing list