开发者

Qt + search engine API

I need google (or other engine) search in my desktop program. Could you please give me an example how to send a POST request to API and get the answer. I use Qt and C++.

For example this code doesn't work (it's about yandex API):

QString* query = new QString("<?xml version=""1.0"" encoding=""UTF-8""?>"
                         开发者_StackOverflow    "<request><query>" + ui->search_le->text().toUtf8() +
                             "</query><groupings><groupby attr=""d"""
                             "mode=""deep""groups-on-page=""10""docs-in-group=""1"" />"
                             "</groupings></request>");

QUrl apiurl = QUrl("http://xmlsearch.yandex.ru/xmlsearch?user=*******&"
                   "key=03.*******:**************f01e29f007af7994e951");

manager = new QNetworkAccessManager();
request = new QNetworkRequest(apiurl);
reply = manager->post(*request, query->toUtf8());

QString answer = QString::fromUtf8(reply->readAll());

And I really don't know how to find the problem.

Thanks all who will be able to help me.


You are on the right track. Read about Signals and slots in Qt. http://doc.qt.io/archives/qt-4.7/signalsandslots.html

You have to connect QNetworkAccessManager's finished() signal to your own slot and then call reply->readAll();

Do this before you call the post method.

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

Then implement the replyFinished method in your class.

MyClass::replyFinished(QNetworkReply* reply)
{
 QString answer = QString::fromUtf8(reply->readAll());     
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜