• 3628阅读
  • 2回复

如何保存QNetworkAccessManager::get()得到的数据? [复制链接]

上一主题 下一主题
离线yearl
 

只看楼主 倒序阅读 楼主  发表于: 2012-11-13
网上的这个例子。
void Http::finishedSlot(QNetworkReply* reply)
{
    // Reading attributes of the reply
    // e.g. the HTTP status code
    QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    // Or the target URL if it was a redirect:
    QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    // see CS001432 on how to handle this
    // no error received?
    if (reply->error() == QNetworkReply::NoError)
    {
        // Example 2: Reading bytes form the reply
        QByteArray bytes = reply->readAll();  // bytes
//        QString string(bytes); // string
        recieve = QString::fromUtf8(bytes);
        //        recieve = QString::fromUtf8(bytes);
//        qDebug() << recieve;
    }
    // Some http error received
    else
    {
        // handle errors here
    }
    // We receive ownership of the reply object
    // and therefore need to handle deletion.
    reply->deleteLater();
}
在程序中服务返回的结果(recieve)被直接处理了(显示出来了)。我想将这个结果保存起来,我定义了一个实例变量recieve。

可是我怎样才能将结果返回到调用它的位置呢?
QUrl url(http://XXXXX);
nam->get(QNetworkRequest(url));
此时recieve还没有返回值啊。

离线yearl

只看该作者 1楼 发表于: 2012-11-13
除了
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();

这个方法外还有没有其它的方法?
离线yearl

只看该作者 2楼 发表于: 2012-11-14
这个方法可以工作,但不是好方法,他阻塞了。
如果真正想写出好代码,应该从进程通讯和同步的方向去考虑。
快速回复
限100 字节
 
上一个 下一个