QNetworkAccessManager不发出信号

时间:2016-05-17 19:53:55

标签: c++ qt qnetworkaccessmanager

所以我有这段代码:

QUrl url("http://...");
QNetworkRequest request(url);
QNetworkReply *reply = m_networkManager->get(request);
connect(reply, SIGNAL(finished()), SLOT(onRequestCompleted()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),SLOT(onError(QNetworkReply::NetworkError)));

我无法向其他功能发出信号

void IpResolver::onRequestCompleted()
{
QString webContent;

QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());

if (reply)
{
    if (reply->error() == QNetworkReply::NoError)
    {
        QString webContent = reply->readAll();
    }
}
}

我无法找到解决方案,请帮助。

1 个答案:

答案 0 :(得分:1)

我不知道你到底想要什么,但是:

  1. 为什么使用回复指针而不是某种onRequestCompleted(QNetworkReply *reply)
  2. 如果您这样做:

    QUrl url("http://...");
    QNetworkRequest request(url);
    connect(m_networkManager, &QNetworkAccessManager::finished, this, &IpResolver::onRequestCompleted);
    m_networkManager->get(request);
    
  3. 您的广告位将是,例如:

    void IpResolver::onRequestCompleted(QNetworkReply *reply)
    {
        QString webContent;
        if (reply->error() == QNetworkReply::NoError)
            webContent = reply->readAll();
    }