简体   繁体   中英

Using Libcurl properly in Qt application

I am new to both Qt and libcurl. I now need to use libcurl in my Qt application. I started with:

m_res = curl_easy_perform(m_curl);

Then I emit a signal which gives this string to another application

emit dataString (QString::fromStdString(dataBufferfromcurl));

I have connected this signal to an slot in other application. But when the curl is called, it's blocking the other program by reading data. Can someone suggest me how to use licurl properly in a Qt application?

Here's some more-complete code:

Class A : public QObject{
void init(){
 if (m_curl){
   curl_easy_setopt(m_curl, CURLOPT_URL, "www.example.com"); 
   curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, 0L); // enabling ssl 
   curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYHOST, 0L);
   curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, 0); 
   curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, WriteCallback);
   curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, this);//

}
void process(){
  if (m_curl) {     
    m_res = curl_easy_perform(m_curl);
  }
};

//*************************************************************

class B : QThread{
  Q_OBJECT
public:
  void runProcess(){
    aObject = new A(); 
    ///************ few more connects
    connect(aObject, SIGNAL(connectionError(QString)), this, SLOT(s_fireConnectionError(QString)), Qt::QueuedConnection);
    QTimer::singleShot(1000, workingHTTPReader, SLOT(process()));
  }    
} ;

In class C, I call B::runProcess() :

void C:: getAllData()
{
    bObject = new B;
    bObject->runProcess(); //***********Problem     
}

If I call it this way, it reads the web page content, but execution of the rest of the application blocks.

How can I call this without blocking?

aObject 's thread() is thread b, and thread b not have evnet loop.. you must start event loop in the thread. If a QObject has no thread affinity (that is, if thread() returns zero), or if it lives in a thread that has no running event loop, then it cannot receive queued signals or posted events.

You call B::runProcess() in the same thread as the rest of the application. Creating a class that inherits from QThread does not automatically make all its methods run in a different thread.

For your purposes, the easiest way to achieve what you're after is to create a thread pool (or, more simply, use the application's default thread pool), and queue your method to execute there:

void C:: getAllData()
{
    auto b = new B;
    b->setAutoDelete(true); // if you're not deleting it yourself later
    QThreadPool::globalInstance()->start(hello);   
}

Class B must inherit from QRunnable instead of QThread :

class B : public QRunnable
{
public:
    void run() Q_DECL_OVERRIDE;
}

This is just an outline solution; you'll want to read the Qt documentation of QRunnable and QThreadPool to get a full understanding of what you're doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM