[Interest] Deleting the QNetworkAccessManager post(req, iodev) QIODevice?

Julien Cugnière julien.cugniere at gmail.com
Wed Mar 2 19:33:16 CET 2016


And yet another idea (untested), using C++11 lambdas :

    QFile *f = new QFile(filename);
    f->open(QIODevice::ReadOnly);

    QNetworkReply* reply = nam.post(req, f);

    connect(reply, &QNetworkReply::finished, f, [=f]() {
        f->close();
        f->remove();
        delete f; // or f->deleteLater()
    });


Julien Cugnière

2016-03-02 19:19 GMT+01:00 Elvis Stansvik <elvstone at gmail.com>:
>
> 2016-03-02 18:56 GMT+01:00 Jason H <jhihn at gmx.com>:
> > Indeed it is, there's a couple ways to skin this cat.
>
> Another idea, after looking at the API: You could perhaps transport
> your QFile* pointer in a user attribute of the request, e.g. something
> like (untested):
>
> int CoolFileAttribute = QNetworkRequest::User;
>
> // Sending the request, attaching the QFile* pointer to it.
> QFile *f = new QFile(filename);
> f->open(QIODevice::ReadOnly)
> ...
> req.setAttribute(CoolFileAttribute, QVariant::fromValue(f));
> nam.post(req, f);
>
> // And in your slot handling the reply, get the attribute from the
> corresponding request.
> QFile *f = reply->request().attribute(CoolFileAttribute).value<QFile*>();
> f->remove();
> f->close();
> delete f;
>
> With obviously some improved naming / error handling.
>
> Elvis
>
> > What I settled on (so far) is since I'm using setParent(), isto iterate over
> > the children and just QFile::remove() anything that can be cast as a QFile*.
> > It eliminates maintaining an additional mapping. it seems that this idea of
> > having a file that is to be deleted on request does not exist, except for
> > QTemporaryFile which you can't explitily state the filename.
> >
> >> Let me expand on this, I can f->setParent(reply), but I also need to
> >> delete the file from disk. If I rely on the parent/child object deletion, I
> >> can't get a change to delete the file.
> >>
> >> > QFile *f = new QFile(filename);
> >> > f->open(QIODevice::ReadOnly)
> >> > ...
> >> > nam.post(req, f);
> >> > ...
> >> >
> >> > // sometime later in the finished slot:
> >> > finished(QNetworkReply *reply) {
> >> >
> >> >  // how to close and delete f ? (The QFile object)
> >
> > Save the pointer somewhere when you create it, then use the saved pointer to
> > close/delete it?
> >
> > I guess you would need to store it indexed by the request, so that you know
> > which file to close/delete in your slot.
> >
> > Take my advice with some grain of salt, as I haven't used QNAM before. But
> > it sounds like a general resource management problem.
> >
> > Cheers,
> > Elvis
> >
> >
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list