Re: [Interest] QNetwork classes for submitting google forms

2021-06-14 Thread Jason H


> Sent: Monday, June 14, 2021 at 2:07 PM
> From: "Jason H" 
> To: "Jason H" 
> Cc: "Scott Bloom" , "Thiago Macieira" 
> , "interest@qt-project.org" 
> 
> Subject: Re: [Interest] QNetwork classes for submitting google forms
>
> 
> 
> > Sent: Monday, June 14, 2021 at 1:42 PM
> > From: "Jason H" 
> > To: "Scott Bloom" 
> > Cc: "Thiago Macieira" , 
> > "interest@qt-project.org" 
> > Subject: Re: [Interest] QNetwork classes for submitting google forms
> >
> > 
> > 
> > > Sent: Monday, June 14, 2021 at 1:12 PM
> > > From: "Scott Bloom" 
> > > To: "Thiago Macieira" , 
> > > "interest@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:

https://gist.github.com/jhihn/19a4f03eeae473f3083dcecb4e25ad59

___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-14 Thread Jason H


> Sent: Monday, June 14, 2021 at 1:42 PM
> From: "Jason H" 
> To: "Scott Bloom" 
> Cc: "Thiago Macieira" , "interest@qt-project.org" 
> 
> Subject: Re: [Interest] QNetwork classes for submitting google forms
>
> 
> 
> > Sent: Monday, June 14, 2021 at 1:12 PM
> > From: "Scott Bloom" 
> > To: "Thiago Macieira" , 
> > "interest@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 lambda) {
QEventLoop loop; 
QNetworkReply * reply = remoteCloud->get(endpoint);
QObject::connect(reply, ::finished, , 
::quit); 
QObject::connect(reply, ::finished, reply, [=]{
lambda(reply);
reply->deleteLater(); 
});
loop.exec(); // wait for finished before returning
}


void RemoteCloud::getFile(const QString& endpoint, std::function lambda) {
QNetworkReply * reply = remoteCloud->get(endpoint);
QObject::connect(reply, ::finished, reply, [=]{
lambda(reply);
reply->deleteLater(); // always cleanup after the lambda ran
});
}
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-14 Thread Jason H


> Sent: Monday, June 14, 2021 at 1:12 PM
> From: "Scott Bloom" 
> To: "Thiago Macieira" , "interest@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.


___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-14 Thread Scott Bloom
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.

Scott

-Original Message-
From: Interest  On Behalf Of Thiago Macieira
Sent: Sunday, June 13, 2021 08:03
To: interest@qt-project.org
Subject: Re: [Interest] QNetwork classes for submitting google forms

On Friday, 11 June 2021 21:05:08 PDT Max Paperno wrote:
> > Insert a "return" here and let your slot be called when the time is right.
> 
> Right, too much Python lately... "should" have been `processEvents()` 
> which is when I realized there were no events to process w/out a Qt 
> loop in the first place.  Returning from main() wouldn't have solved 
> the issue though.

Please do as I said: insert a return and let the event loop handle calling your 
slots.

Nested event loops are an anti-pattern. Don't write code like that unless you 
really must. And if you do, use QEventLoop.

Don't use processEvents(). That's only slightly less evil than sleep().

--
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-14 Thread Thiago Macieira
On Sunday, 13 June 2021 12:23:58 PDT Max Paperno wrote:
> Huh? In the source code somewhere it says "don't use processEvents()?"
> Seems maybe that belongs in the docs...   I understand what it means
> to read the source to see how something works, but I'm not sure how that
> applies here.

I said it *is* the source code, not "in the source code". You have to 
interpret what it does and also what it does not do.

> > QEventLoop calls processEvents for you, but it also reacts to quit() and
> > other event-interruption mechanisms, as well as interrupt-prevention
> > mechanisms. If you call processEvents() on your own, you have to provide
> > your own mechanism. Many an application got stuck calling processEvents()
> > after being asked to exit that loop.
> > 
> > If you think you need to exclude UI events, you're wrong.
> > 
> > If you think you want to process a limited number of events (especially
> > "just one"), you're wrong.
> 
> Thanks for elaborating. So the actual answer/advice is to use
> processEvents() _correctly_ (or QEventLoop which "autmagically" does the
> "right thing" for you... read its the code to see what that is). Which
> is certainly a valid point.  I will amend any future mention if it with
> a big *"when used correctly" footnote instead of assuming that someone
> would already do that in the first place.

The actual advice is to not nest event loops. Return to the outer event loop 
(assuming it is started, of course). This applies to modal dialogues too: 
show() and return to the event loop.

If you have to start a new event loop level, then use the exec() functions in 
QCoreApplication, QThread or QEventLoop.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-14 Thread Jason H


> Sent: Monday, June 14, 2021 at 10:36 AM
> From: "Jason H" 
> To: "Max Paperno" 
> Cc: interest@qt-project.org
> Subject: Re: [Interest] QNetwork classes for submitting google forms
>
> You might want to look at my pos on Jun 2 "Re: [Interest] QNetworkReply 
> lambdas?"
>
> Where I give example code on how to set up a lambda. You can ignore the 
> synchronous parts.

After reading more replies on this thread, you might want to look at the 
synchronous parts on how to properly wield an event loop in this situation.


___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-14 Thread Jason H
You might want to look at my pos on Jun 2 "Re: [Interest] QNetworkReply 
lambdas?"

Where I give example code on how to set up a lambda. You can ignore the 
synchronous parts.

> Sent: Friday, June 11, 2021 at 8:29 PM
> From: "Max Paperno" 
> To: interest@qt-project.org
> Subject: Re: [Interest] QNetwork classes for submitting google forms
>
> Ah yeah, it would help a lot to have a Qt event loop to actually deliver 
> the signals... silly me. Also forgot to delete the reply as per docs.
> 
> Tested, works:
> 
> 
> int main(int argc, char**argv)
> {
>    QCoreApplication app(argc, argv);
>    QUrlQuery postData;
>    postData.addQueryItem("entry.2020959411", "Qt Query");
> 
>    QUrl serviceUrl("http://httpbin.org/post;);
>    QNetworkRequest request(serviceUrl);
>    QNetworkAccessManager networkManager;
> 
>    QObject::connect(, ::finished,
>     [&](QNetworkReply *reply) {
>    int status = 
> reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
>    qDebug() << "Got status:" << status << "Data:" << reply->readAll();
>    //  etc
>    reply->deleteLater();
>    app.exit();
>    });
> 
>    networkManager.post(request,
> postData.toString(QUrl::FullyEncoded).toUtf8());
> 
>    return app.exec();
> }
> 
> 
> 
> Please reply to the list, not to me, thanks.
> 
> HTH,
> -Max
> 
> 
> On 6/11/2021 6:53 PM, Nicholas Yue wrote:
> > ```
> > intmain()
> > {
> > QUrlQuerypostData;
> > postData.addQueryItem("emailAddress","abc@gmail.com 
> > <mailto:abc@gmail.com>");
> > postData.addQueryItem("entry.2020959411","QtQuery");
> > QUrlserviceUrl("https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/formResponse;);
> > QNetworkRequestrequest(serviceUrl);
> > QNetworkAccessManagernetworkManager;
> > boolgotResponse=false;
> > QObject::connect(,::finished,
> > [](QNetworkReply*reply){
> > intstatus=reply->attribute(
> > QNetworkRequest::HttpStatusCodeAttribute).toInt();
> > qDebug()< > //etc
> > gotResponse=true;
> > });
> > networkManager.post(request,
> > postData.toString(QUrl::FullyEncoded).toUtf8());
> > while(!gotResponse){
> > QThread::sleep(1);//orwhateversleepmethod,justwaitingforaresponse.
> > qDebug()<<"Slept1second";
> > }
> > return0;
> > }
> > ```
> > It just sits there, I get the regular print out about 'Slept 1 second', 
> > other than that, it does not seems to be working
> >
> > On Fri, 11 Jun 2021 at 14:18, Max Paperno  > <mailto:ma...@wdg.us>> wrote:
> >
> >
> > > QObject::connect(, ::finished, ...
> >
> > Whoops, should really be
> > QObject::connect(,
> > ::finished, ...
> >
> > -Max
> > ___
> > Interest mailing list
> > Interest@qt-project.org <mailto:Interest@qt-project.org>
> > https://lists.qt-project.org/listinfo/interest
> >
> >
> >
> > -- 
> > Nicholas Yue
> > Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
> > Custom Dev - C++ porting, OSX, Linux, Windows
> > http://au.linkedin.com/in/nicholasyue
> > https://vimeo.com/channels/naiadtools
> 
> 
> ___
> Interest mailing list
> Interest@qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-13 Thread Max Paperno



On 6/13/2021 1:08 PM, Thiago Macieira wrote:

That was a toy example application. Most applications will post in event to
something happening, so the event loop has already started.


But we weren't discussing "most applications."  The OP asked how to 
handle a network request and presented an MRE.  Or are you saying I 
should have written a full app for them?  Are you like this on 
StackOverflow also?



In the particular you need app.exec().


You mean like my second working example? I appreciate your pointing out 
my mistake (several hours after I corrected it) and the general warning 
about sleeping, but now the horse is already dead.



Don't use processEvents(). That's only slightly less evil than sleep().


Maybe you could expand on that further instead of just making blanket
statements?  Reference?


The reference is the source code.


Huh? In the source code somewhere it says "don't use processEvents()?" 
Seems maybe that belongs in the docs... :)  I understand what it means 
to read the source to see how something works, but I'm not sure how that 
applies here.



QEventLoop calls processEvents for you, but it also reacts to quit() and other
event-interruption mechanisms, as well as interrupt-prevention mechanisms. If
you call processEvents() on your own, you have to provide your own mechanism.
Many an application got stuck calling processEvents() after being asked to
exit that loop.

If you think you need to exclude UI events, you're wrong.

If you think you want to process a limited number of events (especially "just
one"), you're wrong.


Thanks for elaborating. So the actual answer/advice is to use 
processEvents() _correctly_ (or QEventLoop which "autmagically" does the 
"right thing" for you... read its the code to see what that is). Which 
is certainly a valid point.  I will amend any future mention if it with 
a big *"when used correctly" footnote instead of assuming that someone 
would already do that in the first place.


Is the condescending tone really necessary to get your point across?

Regards,
-Max
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-13 Thread Thiago Macieira
On Sunday, 13 June 2021 08:23:30 PDT Max Paperno wrote:
> > Please do as I said: insert a return and let the event loop handle calling
> > your slots.
> 
> Did you even look at the (bad) code in question? There was no event loop
> and nothing to return from except main().

That was a toy example application. Most applications will post in event to 
something happening, so the event loop has already started.

In the particular you need app.exec().

> > Don't use processEvents(). That's only slightly less evil than sleep().
> 
> Maybe you could expand on that further instead of just making blanket
> statements?  Reference?

The reference is the source code.

QEventLoop calls processEvents for you, but it also reacts to quit() and other 
event-interruption mechanisms, as well as interrupt-prevention mechanisms. If 
you call processEvents() on your own, you have to provide your own mechanism. 
Many an application got stuck calling processEvents() after being asked to 
exit that loop.

If you think you need to exclude UI events, you're wrong.

If you think you want to process a limited number of events (especially "just 
one"), you're wrong.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-13 Thread Max Paperno


On 6/13/2021 11:02 AM, Thiago Macieira wrote:

On Friday, 11 June 2021 21:05:08 PDT Max Paperno wrote:
Insert a "return" here and let your slot be called when the time is 
right.


Right, too much Python lately... "should" have been `processEvents()`
which is when I realized there were no events to process w/out a Qt loop
in the first place. Returning from main() wouldn't have solved the
issue though.


Please do as I said: insert a return and let the event loop handle calling
your slots.


Did you even look at the (bad) code in question? There was no event loop 
and nothing to return from except main().



Don't use processEvents(). That's only slightly less evil than sleep().


Maybe you could expand on that further instead of just making blanket 
statements?  Reference?


-Max


___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-13 Thread Thiago Macieira
On Friday, 11 June 2021 21:05:08 PDT Max Paperno wrote:
> > Insert a "return" here and let your slot be called when the time is right.
> 
> Right, too much Python lately... "should" have been `processEvents()`
> which is when I realized there were no events to process w/out a Qt loop
> in the first place.  Returning from main() wouldn't have solved the
> issue though.

Please do as I said: insert a return and let the event loop handle calling 
your slots.

Nested event loops are an anti-pattern. Don't write code like that unless you 
really must. And if you do, use QEventLoop.

Don't use processEvents(). That's only slightly less evil than sleep().

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-12 Thread Max Paperno


On 6/12/2021 1:29 PM, Nicholas Yue wrote:
I have now moved the code into a small UI test app so there is already a 
Qt loop in the main app but it stopped working (status code not printed 
out) again, do I have to retain the app.exec() and app.exit() ?


Probably because your networkManager goes out of scope as soon as the 
doSubmit() function returns.


My example was for a quick one-file demo, not a real application. You 
have to figure out how to use signals and slots properly, among other 
things. And no, of course you wouldn't put an app.exec() into your UI 
form.  You _could_ app.exit() from a signal handler (or wherever), but 
I'm guessing that's not what you want.  I would urge you to understand 
what each part of an example does before just sticking it in some other 
code.


Regards,
-Max





```

#include"GForm.h"

#include"ui_form.h"

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include



#include


GForm::GForm(QWidget*parent)

:QWidget(parent)

,ui(newUi::Form)

{

ui->setupUi(this);

connect(ui->cancel_pushButton,SIGNAL(clicked()),this,SLOT(close()));

connect(ui->submit_pushButton,SIGNAL(clicked()),this,SLOT(doSubmit()));

}


GForm::~GForm()

{

deleteui;

}


voidGForm::doSubmit()

{

QStringname=ui->name_lineEdit->text();

QStringmessage=ui->message_lineEdit->text();

QStringemail=ui->email_lineEdit->text();


qDebug()attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

qDebug()<<"Gotstatus:"deleteLater();

});


networkManager.post(request,

postData.toString(QUrl::FullyEncoded).toUtf8());



}

}

```

___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-12 Thread Nicholas Yue
I figured it out,

QNetworkAccessManager networkManager;


needs to be a member of the class (not within the doSubmit() method),
otherwise it would have gone out of scope before the reply returns


Cheers



On Sat, 12 Jun 2021 at 10:29, Nicholas Yue  wrote:

> I have now moved the code into a small UI test app so there is already a
> Qt loop in the main app but it stopped working (status code not printed
> out) again, do I have to retain the app.exec() and app.exit() ?
>
> ```
>
> #include "GForm.h"
>
> #include "ui_form.h"
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
> #include 
>
>
>
> #include 
>
>
> GForm::GForm(QWidget *parent)
>
> : QWidget(parent)
>
> , ui(new Ui::Form)
>
> {
>
> ui->setupUi(this);
>
> connect( ui->cancel_pushButton, SIGNAL(clicked()), this, SLOT(close() ) );
>
> connect( ui->submit_pushButton, SIGNAL(clicked()), this, SLOT(doSubmit() 
> ) );
>
> }
>
>
> GForm::~GForm()
>
> {
>
> delete ui;
>
> }
>
>
> void GForm::doSubmit()
>
> {
>
> QString name = ui->name_lineEdit->text();
>
> QString message = ui->message_lineEdit->text();
>
> QString email = ui->email_lineEdit->text();
>
>
> qDebug() << QString("Do Submission [message = %1, name = %2, email = 
> %3]").arg(message).arg(name).arg(email);
>
>
> {
>
> // Google form submission
>
>
> QUrlQuery postData;
>
> postData.addQueryItem("entry.305082368", message);
>
> postData.addQueryItem("entry.1264643879", name);
>
> postData.addQueryItem("entry.1004643569", email);
>
>
> QUrl 
> serviceUrl("https://docs.google.com/forms/d/1ngIkIaj0CEdJl1ucL9JgVq82rUquPbKPGt4066bKscA/formResponse;);
>
> QNetworkRequest request(serviceUrl);
>
> request.setHeader(QNetworkRequest::ContentTypeHeader,
>
>   "application/x-www-form-urlencoded");
>
> QNetworkAccessManager networkManager;
>
>
> QObject::connect(, ::finished,
>
>  [&](QNetworkReply *reply) {
>
> int status =
>
> 
> reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
>
> qDebug() << "Got status:" << status << "Data:" << 
> reply->readAll();
>
> //  etc
>
> reply->deleteLater();
>
> });
>
>
> networkManager.post(request,
>
> postData.toString(QUrl::FullyEncoded).toUtf8());
>
>
>
> }
>
> }
>
> ```
>
> On Fri, 11 Jun 2021 at 21:07, Max Paperno  wrote:
>
>>
>> On 6/11/2021 10:32 PM, Thiago Macieira wrote:
>> > On Friday, 11 June 2021 14:10:57 PDT Max Paperno wrote:
>> >> while (!gotResponse)
>> >> sleep(1)  // or whatever sleep method, just waiting for a response.
>> >
>> > NEVER EVER sleep.
>> >
>> > Insert a "return" here and let your slot be called when the time is
>> right.
>> >
>>
>> Right, too much Python lately... "should" have been `processEvents()`
>> which is when I realized there were no events to process w/out a Qt loop
>> in the first place.  Returning from main() wouldn't have solved the
>> issue though.
>>
>> -Max
>> ___
>> Interest mailing list
>> Interest@qt-project.org
>> https://lists.qt-project.org/listinfo/interest
>>
>
>
> --
> Nicholas Yue
> Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
> Custom Dev - C++ porting, OSX, Linux, Windows
> http://au.linkedin.com/in/nicholasyue
> https://vimeo.com/channels/naiadtools
>


-- 
Nicholas Yue
Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
Custom Dev - C++ porting, OSX, Linux, Windows
http://au.linkedin.com/in/nicholasyue
https://vimeo.com/channels/naiadtools
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-12 Thread Nicholas Yue
I have now moved the code into a small UI test app so there is already a Qt
loop in the main app but it stopped working (status code not printed out)
again, do I have to retain the app.exec() and app.exit() ?

```

#include "GForm.h"

#include "ui_form.h"

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 



#include 


GForm::GForm(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Form)

{

ui->setupUi(this);

connect( ui->cancel_pushButton, SIGNAL(clicked()), this, SLOT(close() ) );

connect( ui->submit_pushButton, SIGNAL(clicked()), this,
SLOT(doSubmit() ) );

}


GForm::~GForm()

{

delete ui;

}


void GForm::doSubmit()

{

QString name = ui->name_lineEdit->text();

QString message = ui->message_lineEdit->text();

QString email = ui->email_lineEdit->text();


qDebug() << QString("Do Submission [message = %1, name = %2, email
= %3]").arg(message).arg(name).arg(email);


{

// Google form submission


QUrlQuery postData;

postData.addQueryItem("entry.305082368", message);

postData.addQueryItem("entry.1264643879", name);

postData.addQueryItem("entry.1004643569", email);


QUrl 
serviceUrl("https://docs.google.com/forms/d/1ngIkIaj0CEdJl1ucL9JgVq82rUquPbKPGt4066bKscA/formResponse;);

QNetworkRequest request(serviceUrl);

request.setHeader(QNetworkRequest::ContentTypeHeader,

  "application/x-www-form-urlencoded");

QNetworkAccessManager networkManager;


QObject::connect(, ::finished,

 [&](QNetworkReply *reply) {

int status =


reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

qDebug() << "Got status:" << status << "Data:" << reply->readAll();

//  etc

reply->deleteLater();

});


networkManager.post(request,

postData.toString(QUrl::FullyEncoded).toUtf8());



}

}

```

On Fri, 11 Jun 2021 at 21:07, Max Paperno  wrote:

>
> On 6/11/2021 10:32 PM, Thiago Macieira wrote:
> > On Friday, 11 June 2021 14:10:57 PDT Max Paperno wrote:
> >> while (!gotResponse)
> >> sleep(1)  // or whatever sleep method, just waiting for a response.
> >
> > NEVER EVER sleep.
> >
> > Insert a "return" here and let your slot be called when the time is
> right.
> >
>
> Right, too much Python lately... "should" have been `processEvents()`
> which is when I realized there were no events to process w/out a Qt loop
> in the first place.  Returning from main() wouldn't have solved the
> issue though.
>
> -Max
> ___
> Interest mailing list
> Interest@qt-project.org
> https://lists.qt-project.org/listinfo/interest
>


-- 
Nicholas Yue
Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
Custom Dev - C++ porting, OSX, Linux, Windows
http://au.linkedin.com/in/nicholasyue
https://vimeo.com/channels/naiadtools
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-11 Thread Max Paperno


On 6/11/2021 10:32 PM, Thiago Macieira wrote:

On Friday, 11 June 2021 14:10:57 PDT Max Paperno wrote:

while (!gotResponse)
sleep(1)  // or whatever sleep method, just waiting for a response.


NEVER EVER sleep.

Insert a "return" here and let your slot be called when the time is right.



Right, too much Python lately... "should" have been `processEvents()` 
which is when I realized there were no events to process w/out a Qt loop 
in the first place.  Returning from main() wouldn't have solved the 
issue though.


-Max
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-11 Thread Thiago Macieira
On Friday, 11 June 2021 14:10:57 PDT Max Paperno wrote:
> while (!gotResponse)
>sleep(1)  // or whatever sleep method, just waiting for a response.

NEVER EVER sleep.

Insert a "return" here and let your slot be called when the time is right.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-11 Thread Max Paperno
Ah yeah, it would help a lot to have a Qt event loop to actually deliver 
the signals... silly me. Also forgot to delete the reply as per docs.


Tested, works:


int main(int argc, char**argv)
{
  QCoreApplication app(argc, argv);
  QUrlQuery postData;
  postData.addQueryItem("entry.2020959411", "Qt Query");

  QUrl serviceUrl("http://httpbin.org/post;);
  QNetworkRequest request(serviceUrl);
  QNetworkAccessManager networkManager;

  QObject::connect(, ::finished,
   [&](QNetworkReply *reply) {
  int status = 
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

  qDebug() << "Got status:" << status << "Data:" << reply->readAll();
  //  etc
  reply->deleteLater();
  app.exit();
  });

  networkManager.post(request,
postData.toString(QUrl::FullyEncoded).toUtf8());

  return app.exec();
}



Please reply to the list, not to me, thanks.

HTH,
-Max


On 6/11/2021 6:53 PM, Nicholas Yue wrote:

```
intmain()
{
QUrlQuerypostData;
postData.addQueryItem("emailAddress","abc@gmail.com 
");
postData.addQueryItem("entry.2020959411","QtQuery");
QUrlserviceUrl("https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/formResponse;);
QNetworkRequestrequest(serviceUrl);
QNetworkAccessManagernetworkManager;
boolgotResponse=false;
QObject::connect(,::finished,
[](QNetworkReply*reply){
intstatus=reply->attribute(
QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug()> wrote:



> QObject::connect(, ::finished, ...

Whoops, should really be
QObject::connect(,
::finished, ...

-Max
___
Interest mailing list
Interest@qt-project.org 
https://lists.qt-project.org/listinfo/interest



--
Nicholas Yue
Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
Custom Dev - C++ porting, OSX, Linux, Windows
http://au.linkedin.com/in/nicholasyue
https://vimeo.com/channels/naiadtools



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-11 Thread Nicholas Yue
```

int main()

{


QUrlQuery postData;

postData.addQueryItem("emailAddress", "abc@gmail.com");

postData.addQueryItem("entry.2020959411", "Qt Query");


QUrl 
serviceUrl("https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/formResponse;);

QNetworkRequest request(serviceUrl);



QNetworkAccessManager networkManager;

bool gotResponse = false;


QObject::connect(, ::finished,

 [](QNetworkReply *reply) {

int status = reply->attribute(

QNetworkRequest::HttpStatusCodeAttribute).toInt();

qDebug() << status;

//  etc

gotResponse = true;

});


networkManager.post(request,

postData.toString(QUrl::FullyEncoded).toUtf8());


while (!gotResponse) {

QThread::sleep(1); // or whatever sleep method, just waiting
for a response.

qDebug() << "Slept 1 second";

}


return 0;


}

```


It just sits there, I get the regular print out about 'Slept 1
second', other than that, it does not seems to be working


On Fri, 11 Jun 2021 at 14:18, Max Paperno  wrote:

>
> > QObject::connect(, ::finished, ...
>
> Whoops, should really be
> QObject::connect(, ::finished, ...
>
> -Max
> ___
> Interest mailing list
> Interest@qt-project.org
> https://lists.qt-project.org/listinfo/interest
>


-- 
Nicholas Yue
Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
Custom Dev - C++ porting, OSX, Linux, Windows
http://au.linkedin.com/in/nicholasyue
https://vimeo.com/channels/naiadtools
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-11 Thread Max Paperno



QObject::connect(, ::finished, ...


Whoops, should really be
QObject::connect(, ::finished, ...

-Max
___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] QNetwork classes for submitting google forms

2021-06-11 Thread Max Paperno

Hello Nicholas,


I would like to know if I am using the Qt QNetwork classes correctly
to submit a post to a URL that is a Google form


From the docs[1]: QNetworkAccessManager has an asynchronous API.

That means you have to wait for a finished() signal before trying to 
determine the status or read response data. The call to post() returns 
right away, probably before the request is even sent.


The docs have a pretty basic example, but to put it into your context, 
it would look something like this:


// ...
QNetworkAccessManager networkManager;
bool gotResponse = false;

QObject::connect(, ::finished, 
[](QNetworkReply *reply) {

  int status = reply->attribute(
  QNetworkRequest::HttpStatusCodeAttribute).toInt();
  qDebug() << status;
  //  etc
  gotResponse = true;
});

networkManager.post(request, 
postData.toString(QUrl::FullyEncoded).toUtf8());


while (!gotResponse)
  sleep(1)  // or whatever sleep method, just waiting for a response.

return 0;
}


HTH,
-Max


[1]: https://doc.qt.io/qt-5/qnetworkaccessmanager.html


On 6/11/2021 4:22 PM, Nicholas Yue wrote:

Hi,

I am trying to submit google forms programmatically via C++ and Qt classes

I am basing my study on the following

https://stackoverflow.com/questions/17964429/google-forms-response-with-python#17965510

I got the Python version running and returned a 200 response.

I tried converting the code to C++ and Qt but was not successful. I get 
a response of '0' zero


I would like to know if I am using the Qt QNetwork classes correctly to 
submit a post to a URL that is a Google form


=
#include 
#include 
#include 
#include 
#include 

int main()
{

     QUrlQuery postData;
     postData.addQueryItem("emailAddress", "abc@gmail.com 
");

     postData.addQueryItem("entry.2020959411", "Qt Query");

     /*
      *
     url = 
'https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/formResponse'

     form_data = {'entry.2020959411': '18+ sollte absolute Pflicht sein',
                  #'entry.2020959411': 'Alter sollte garkeine Rolle 
spielen',

                  #'entry.2020959411': '17+ wäre für mich vertretbar',
                  #'entry.2020959411': '16+ wäre für mich vertretbar',
                  #'entry.2020959411': '15+ wäre für mich vertretbar',
                  #'entry.2020959411': 'Ausnahmen von der Regel - Dafür?',
                  #'entry.2020959411': 'Ausnahmen von der Regel - Dagegen?',
                  #'entry.2020959411': '__other_option__',
                  #'entry.2020959411.other_option_response': 'test',
                  'draftResponse': [],
                  'pageHistory': 0}     *
      */

     QUrl 
serviceUrl("https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/formResponse;);


     // ...
     QNetworkRequest request(serviceUrl);
     //request.setHeader(QNetworkRequest::ContentTypeHeader,
     //    "application/x-www-form-urlencoded");
     QNetworkAccessManager networkManager;
     QNetworkReply* reply;
     reply = networkManager.post(request, 
postData.toString(QUrl::FullyEncoded).toUtf8());
     QVariant statusCode = reply->attribute( 
QNetworkRequest::HttpStatusCodeAttribute );

     int status = statusCode.toInt();
     qDebug() << status;
     if ( status != 200 )
     {
         QString reason = reply->attribute( 
QNetworkRequest::HttpReasonPhraseAttribute ).toString();

         qDebug() << reason;
     }

return 0;
}

--
Nicholas Yue
Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
Custom Dev - C++ porting, OSX, Linux, Windows
http://au.linkedin.com/in/nicholasyue
https://vimeo.com/channels/naiadtools

___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest