QNetworkaccessmanager对象连接函数未调用

时间:2018-11-15 07:40:07

标签: c++ qt qt5 qnetworkaccessmanager

在我的项目中,我想从服务器下载文件。我正在尝试使用 Qnetwrokaccessmanager 下载文件。在我的代码中,我正在使用类似

的连接函数
connect(manger_, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinshed(QNetworkReply*)))

在项目执行时不调用函数replayFinshed()

manger_= new QNetworkAccessManager(this);
connect(manger_, SIGNAL(&QNetworkAccessManager::finished), this, SLOT(replyFinshed(QNetworkReply*)));
connect(manger_, SIGNAL(QNetworkReply::error()), SLOT(errmsg(QNetworkReply *)));
QUrl url(str);
QNetworkRequest request;
request.setUrl(url);
rsult = manger_->get(request);



void Cricbang::replyFinished(QNetworkReply* reply){
    QMessageBox::information(this, "replay", "finished");
    if (reply->error())
    {
        qDebug() << "ERROR!";
        qDebug() << reply->errorString();
    }
    else
    {
        QFile *file = new QFile("saved/team.json");
        if (file->open(QFile::Append))
        {
            file->write(reply->readAll());
            file->flush();
            file->close();
        }
        delete file;
    }

    reply->deleteLater();
  }  

1 个答案:

答案 0 :(得分:0)

尝试更改

connect(manger_, SIGNAL(&QNetworkAccessManager::finished), this, SLOT(replyFinshed(QNetworkReply*)));

connect(manger_, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinshed(QNetworkReply*)));

或使用Qt5中引入的新语法,如建议的eyllanesc

connect(manger_, &QNetworkAccessManager::finished, this, &Cricbang::replyFinshed);

https://wiki.qt.io/New_Signal_Slot_Syntax上查看更多信息