Quantcast
Channel: Qt: Using QCoreApplication / QNetworkAccessManager with Shared Dynamic C++ Library - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Qt: Using QCoreApplication / QNetworkAccessManager with Shared Dynamic C++ Library

$
0
0

I am developing a shared library using Qt as the main IDE. The library should be dynamically linked for any application using it and is required to be used for Windows, OSX and Linux with possible mobile platforms planned for the future.

Qt comes loaded with several really helpful libraries like QString (for unicode) and QNetwork. So far I am only using QtCore libraries but found I need to do some REST-calls in the shared library. I quickly found several guides to use QNetworkAccessManager in Qt for REST-calls. However the guides are all based on application templates not libraries. Obviously there's no console, GUI or main function in my shared library.

QtCoreApplication is always in the examples where QNetworkAccessManager is used. From the documentation it's described as:

The QtCoreApplication class provides an event loop for console Qt applications

The way my library is created it contains several independant functions and should never require any console. The function making the REST-call is independant and QCoreApplication and QNetworkAccessManager could very well be created here before being deleted when the result is received.

Is this possible? I have tried to get this working but the program stops when trying to execute the QtCoreApplication:

int argc = 1;char appname[] = "App";char* argv[] = {appname, NULL};QCoreApplication app = new QCoreApplication(QAppPriv::argc, QAppPriv::argv);app->exec();

I had the following code being run in a "MakeRequest"-function but got the error "QEventLoop: Cannot be used without QApplication":

QTimer timer;timer.setSingleShot(true);// Create custom temporary event loop on stackQEventLoop eventLoop;timer.start(5000);// "quit()" the event-loop, when the network request "finished()"QNetworkAccessManager mgr;QObject::connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(serviceRequestFinished(QNetworkReply*)));QUrl url( QString("http://ip.jsontest.com/") );QNetworkReply* reply = mgr.get(QNetworkRequest(url));eventLoop.exec();

Any help are very much appriciated!!!


Viewing all articles
Browse latest Browse all 2

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>