[PyQt] Subclassing QNetworkReply
Detlev Offenbach
detlev at die-offenbachs.de
Mon Mar 2 18:31:26 GMT 2009
On Montag, 2. März 2009, Phil Thompson wrote:
> 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.
That works now. However, I am facing a new problem. After several requests
with a "qthelp" scheme, my application is crashing with a segfault. If I keep
the references to the reply objects (i.e. never clean the array), it works
(but leaks memory). What is wrong?
Note: engine is a reference to a QHelpEngine object.
---------------
class HelpNetworkAccessManager(QNetworkAccessManager):
def __init__(self, engine, parent = None):
QNetworkAccessManager.__init__(self, parent)
self.__engine = engine
self.__helpreplies = []
def createRequest(self, op, request, outgoingData):
for reply in self.__helpreplies[:]:
if reply.isFinished():
self.__helpreplies.remove(reply)
del reply
scheme = request.url().scheme()
if scheme == "qthelp":
reply = HelpNetworkReply(request,
self.__engine.fileData(request.url()))
self.__helpreplies.append(reply)
print len(self.__helpreplies)
return reply
return QNetworkAccessManager.createRequest(self, op, request,
outgoingData)
---------------
Regards,
Detlev
--
Detlev Offenbach
detlev at die-offenbachs.de
More information about the PyQt
mailing list