74738

Qt Console app using QNetworkAccessManager

Question:

I'm trying to write a Qt app that calls a web service. This is a console app, and url will be passed in as a command line argument. I searched for example http programs in Qt and found this link:

<a href="http://qt-project.org/doc/qt-5/qnetworkaccessmanager.html" rel="nofollow">http://qt-project.org/doc/qt-5/qnetworkaccessmanager.html</a>

Here it has the following code example:

QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); manager->get(QNetworkRequest(QUrl("http://qt-project.org")));

Now, if I take this and paste it into my console app, in main, I obviously get build errors because this does not exist. I get :

invalid use of 'this' in non-member function

Is there an equivalent QNetworkAccessManager for non-GUI/console type apps?

Answer1:

"this" is the this pointer of an object, so in main.cpp it causes errors, you should write some class where you will work with network and after that use this class in main function

It should be something like this. When you run app, you'll get html code of Qt site

It is just example, in future we can add here constructot, destructor, maybe signals(signals help us communicate with for example other classes if we need this)

*.h

#ifndef NET_H #define NET_H #include <QObject> #include <QtCore> #include <QNetworkAccessManager> #include <QNetworkReply> class Net : public QObject { Q_OBJECT QNetworkAccessManager *manager; private slots: void replyFinished(QNetworkReply *); public: void CheckSite(QString url); }; #endif // NET_H

*.cpp

#include "net.h" void Net::replyFinished(QNetworkReply *reply) { qDebug() << reply->readAll(); } void Net::CheckSite(QString url) { QUrl qrl(url); manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); manager->get(QNetworkRequest(qrl)); }

main

#include "net.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Net handler; handler.CheckSite("http://qt-project.org"); return a.exec(); }

Recommend

  • Qt Web Service post query
  • VBA: Only import selected columns of a csv file
  • Weird Qt SSL issue — error “No Error” shows up, nothing else, and if I ignore it, everything works
  • Using PyQt4 to return Javascript generated HTML
  • Multiple Screens with Qt
  • What is the parallelization mechanism in QNetworkAccessManager?
  • Qt - How to get responseText with QNetworkAccessmanager
  • redirect to a route after api call in angular2
  • Method to get to the middle of the file for Binary Search
  • How to load files to local file system with vibed?
  • Qt: closing modal dialog closes the program
  • QT Start program as regular user
  • Howto disable nuget machine-wide cache?
  • adding python interpreters programmatically
  • How do I get sun webserver to redirect from /
  • Not quite understanding the query after just shifting column names
  • ZF2: How to get Zend\\Navigation inside custom route?
  • c#.NET USB device persistent identifier
  • iOS two views cover exactly half of parent view
  • I get the following error when trying to set a wallpaper [duplicate]
  • mysql auto kill query
  • How to get or calculate size of Azure File/Share or Service
  • How do I install a previous version of build tools in addition to the sdk?
  • Entity Framework Core 1.0.1 add-migration
  • SharePoint REST query SP.UserProfiles.PeopleManager
  • CoreData basics – to-many relationship array data
  • Neo4j: Legacy Indexes and auto index vs new label bases schema indexes
  • How to stamp out template in self contained custom elements with vanilla js?
  • Fragment gives me an error while inflating 1
  • Strong vs Weak entities MYSQL
  • Using android opencv apps without downloading opencv sdk manager
  • how to avoid repetitive constructor in children
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Saving Changes After In-App Purchase Has Been Purchased
  • How to install a .deb file on a jailbroken iphone programmatically?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?