[Interest] QNetwork classes for submitting google forms
Jason H
jhihn at gmx.com
Mon Jun 14 20:07:30 CEST 2021
> Sent: Monday, June 14, 2021 at 1:42 PM
> From: "Jason H" <jhihn at gmx.com>
> To: "Scott Bloom" <scott at towel42.com>
> Cc: "Thiago Macieira" <thiago.macieira at intel.com>, "interest at qt-project.org" <interest at qt-project.org>
> Subject: Re: [Interest] QNetwork classes for submitting google forms
>
>
>
> > Sent: Monday, June 14, 2021 at 1:12 PM
> > From: "Scott Bloom" <scott at towel42.com>
> > To: "Thiago Macieira" <thiago.macieira at intel.com>, "interest at qt-project.org" <interest at qt-project.org>
> > Subject: Re: [Interest] QNetwork classes for submitting google forms
> >
> > This has come up a couple times for me through the years (essentially make a network request and don’t return until the request has finished).
> >
> > Is there an example anywhere of the "Thiago" (proper.. 😊 ) way to code this? I see this issue in a similar vane to using QThread, when the Trolls/Nokia/TQP (I forget who originally wrote it) wrote up a small but very effective white paper on the proper method of using Qthread, it clarified a ton of questions for many people.
> >
>
>
> You'd probably get feedback that QThread is "too heavy" and for i/o bound processes one async event loop is enough. (I've been on the rx end of that)
>
> I think my highest of levels approach is correct:
> https://lists.qt-project.org/pipermail/interest/attachments/20210602/9366d8ea/attachment-0001.html (apologies for the HTML, but my mailer responds by what ever I am replying to.)
>
> The one caveat to my code, is if you don't need synchronous requests (regrettably, this part of my app requires them) then you can ignore the QEventloop stuff, that is leave lines that references `loop` it out, and you'll have a fully async inline lambda.
>
> You however are asking for a synchronous and this I think is the best way. Feel free to assault the code, (Thiago and others.) I would like Qt to adopt some kind of this pattern.
I cleaned this up a bit:
What to request : http://1270.0.1:4321/file?f=1234 (GET)
What to do: { /* save file */ } or { /* handle error */ }
Invocation:
remoteCloud->synchronousPostFile("http://1270.0.1:4321/file?f=1234"
[=] (QNetworkReply* reply, QString filename) {
if (reply->error() == QNetworkReply::NoError) {
{ /* save file */ }
} else {
{ /* handle error */ }
}
} // end of lambda
); // end of call
void RemoteCloud::synchronousGetFile(const QString& endpoint, std::function<void (QNetworkReply *, const QString &)> lambda) {
QEventLoop loop;
QNetworkReply * reply = remoteCloud->get(endpoint);
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
QObject::connect(reply, &QNetworkReply::finished, reply, [=]{
lambda(reply);
reply->deleteLater();
});
loop.exec(); // wait for finished before returning
}
void RemoteCloud::getFile(const QString& endpoint, std::function<void (QNetworkReply *, const QString &)> lambda) {
QNetworkReply * reply = remoteCloud->get(endpoint);
QObject::connect(reply, &QNetworkReply::finished, reply, [=]{
lambda(reply);
reply->deleteLater(); // always cleanup after the lambda ran
});
}
More information about the Interest
mailing list