[PyQt] Subclassing QNetworkReply
Phil Thompson
phil at riverbankcomputing.com
Mon Mar 2 17:27:24 GMT 2009
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
More information about the PyQt
mailing list