在插件(DLL)中使用Qt网络模块?

时间:2015-11-29 18:11:45

标签: c++ windows qt

我正在为大型非Qt Windows应用程序(Autodesk 3ds Max)编写插件。该插件是用C ++ 11编写的本机DLL。

我希望我的插件通过使用Qt 4.8.7网络组件查询GitHub的HTTP API来检查更新。

我尝试过一个简单的测试,但QNetworkAccessManager::get()电话永远挂起:

QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkReply* reply = manager->get(QNetworkRequest(QUrl("https://api.github.com/repos/appleseedhq/appleseed-max/releases")));
QByteArray result = reply->readAll();

我想这是因为我没有运行Qt事件循环。

在这里进行的正确方法是什么?我需要QtApplication吗?鉴于我正在编写插件而不是应用程序,我应该如何构建内容?

1 个答案:

答案 0 :(得分:1)

解决它的方法是从DLL导出函数中启动新的QThread派生对象并在那里运行事件线程:

    // Mind the files. For MOC compiler to create signals and slots
    // we need the class to be declared in the header file.

    // MyThread.h 
    // Include missing headers
    class MyThread : public QThread
    {
      Q_OBJECT
      protected:
         void run(); // override

      public slots:
         void readData();
         void requestFinished(QNetworkReply * reply);
    };

    void MyThread::run() // override
    {
        // TODO: make it to quit the loop with some signal
        // initialize other objects before the loop
        QNetworkAccessManager* manager = new QNetworkAccessManager(this);
        // we want manager object to run on this thread
        manager->moveToThread(this); // the call maybe redundant here

        connect(manager , SIGNAL(finished(QNetworkReply*)),
            this, SLOT(requestFinished(QNetworkReply*)));

        QEventLoop loop;
        loop.exec();
    }

    // MyThread.cpp
    #include "MyThread.h"
    void MyThread::readData(QString strUrl)
    {
       // mind how exactly you compose the request (?)
       QNetworkRequest request(QUrl(strUrl));

       // here: usually this 'get' request is done from some slot while already in event loop
       QNetworkReply *reply = manager->get(request);
       // or maybe post request
       // QNetworkReply *reply = manager->post(request);

       // Error handling
       // connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
       //   this, SLOT(slotError(QNetworkReply::NetworkError)));
       // connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
       //   this, SLOT(slotSslErrors(QList<QSslError>)));
   }

   void MyThread::requestFinished(QNetworkReply * reply)
   {
      QByteArray result = pReply->readAll();

      // do something with result
      pCallBack(result);
   }


   // MyDll.h
   __declspec(dllexport) void dllFunctionStart();
   // reading new data may depend on your app logic
   __declspec(dllexport) void dllFunctionRequestData();

   // MyDll.cpp 
    #include "MyDll.h"
    #include "MyThread.h"

    static MyThread* s_pThread;

    __declspec(dllexport) void dllFunctionStart()
    {
        // TODO: take care of releasing the thread object on quitting the thread
        s_pThread = new MyThread;
        s_pThread->start();
    }

    // reading new data may depend on your app logic
    __declspec(dllexport) void dllFunctionRequestData()
    {
        if (s_pThread)
            s_pThread->readData();
    }

P.S。我没有编译,但这就是通常解决这个任务的方式。注意待办事项。