[Interest] QNetwork classes for submitting google forms

Max Paperno max-l at wdg.us
Sat Jun 12 19:49:49 CEST 2021


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<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(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()<<QString("DoSubmission[message=%1,name=%2,email=%3]").arg(message).arg(name).arg(email);
> 
> 
> {
> 
> //Googleformsubmission
> 
> 
> QUrlQuerypostData;
> 
> postData.addQueryItem("entry.305082368",message);
> 
> postData.addQueryItem("entry.1264643879",name);
> 
> postData.addQueryItem("entry.1004643569",email);
> 
> 
> QUrlserviceUrl("https://docs.google.com/forms/d/1ngIkIaj0CEdJl1ucL9JgVq82rUquPbKPGt4066bKscA/formResponse");
> 
> QNetworkRequestrequest(serviceUrl);
> 
> request.setHeader(QNetworkRequest::ContentTypeHeader,
> 
> "application/x-www-form-urlencoded");
> 
> QNetworkAccessManagernetworkManager;
> 
> 
> QObject::connect(&networkManager,&QNetworkAccessManager::finished,
> 
> [&](QNetworkReply*reply){
> 
> intstatus=
> 
> reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
> 
> qDebug()<<"Gotstatus:"<<status<<"Data:"<<reply->readAll();
> 
> //etc....
> 
> reply->deleteLater();
> 
> });
> 
> 
> networkManager.post(request,
> 
> postData.toString(QUrl::FullyEncoded).toUtf8());
> 
> 
> 
> }
> 
> }
> 
> ```


More information about the Interest mailing list