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

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

Qt comes with several really useful libraries such as QString (for unicode) and QNetwork. So far I only use the QtCore libraries, but found that I need to make some REST calls to the shared library. I quickly found some tutorials for using QNetworkAccessManager in Qt for REST calls. However, tutorials are based on application templates, not libraries. Obviously there is no console, GUI or main function in my shared library.

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

The QtCoreApplication class provides an event loop for the console of a Qt application

As my library is built it contains several independent functions and never requires a console. The function making the REST call is independent, and QCoreApplication and QNetworkAccessManager can be instantiated here before being dropped when the result is received.

Is it possible? I tried to get this to work, but the program stops when I try to execute 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 executed in the MakeRequest " function , but got the error" QEventLoop: cannot be used without QApplication ":

QTimer timer;
timer.setSingleShot(true);

// Create custom temporary event loop on stack
QEventLoop 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 is very helpful !!!

+3


source to share


1 answer


You can instantiate QCoreApplication

on a new thread in the library. You should check to create only one instance, because every Qt application should only contain one QCoreApplication

:

class Q_DECL_EXPORT SharedLibrary :public QObject    
{
Q_OBJECT
public:
    SharedLibrary();

private slots:

    void onStarted();

private:
    static int argc = 1;
    static char * argv[] = {"SharedLibrary", NULL};
    static QCoreApplication * app = NULL;
    static QThread * thread = NULL;
};


SharedLibrary::SharedLibrary()
{
    if (thread == NULL)
    {
        thread = new QThread();
        connect(thread, SIGNAL(started()), this, SLOT(onStarted()), Qt::DirectConnection);
        thread->start();
    }
}
SharedLibrary::onStarted()
{
   if (QCoreApplication::instance() == NULL)
   {
       app = new QCoreApplication(argc, argv);
       app->exec();
   }
}  

      



This way you can use your shared Qt library in non-Qt applications.

+3


source







All Articles