[Interest] QNetwork classes for submitting google forms

Nicholas Yue yue.nicholas at gmail.com
Sat Jun 12 19:42:29 CEST 2021


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 <yue.nicholas at gmail.com> 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 <QVBoxLayout>
>
> #include <QUiLoader>
>
> #include <QFile>
>
> #include <QApplication>
>
> #include <QDirIterator>
>
> #include <QDebug>
>
> #include <QFile>
>
> #include <QClipboard>
>
> #include <QtNetwork/QNetworkRequest>
>
> #include <QtNetwork/QNetworkAccessManager>
>
> #include <QUrlQuery>
>
> #include <QDebug>
>
> #include <QNetworkReply>
>
> #include <QCoreApplication>
>
> #include <QThread>
>
>
>
> #include <iostream>
>
>
> 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(&networkManager, &QNetworkAccessManager::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 <max-l at wdg.us> 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 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210612/373d4b3b/attachment.html>


More information about the Interest mailing list