QNetworkAccessManager不起作用

时间:2016-05-11 11:09:54

标签: c++ qt qnetworkaccessmanager qnetworkreply qnetworkrequest

我有以下代码:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_networkAccessManager(new QNetworkAccessManager(this))
{
    ui->setupUi(this);

    connect(m_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyForGeoCoordinates(QNetworkReply*)));
    getCoordinates();
}

void MainWindow::getCoordinates()
{
    std::string query = "http://maps.google.com/maps/api/geocode/json?address=warsaw&sensor=false&language=en";
    QNetworkRequest request(QUrl(QString::fromStdString(query)));
    m_reply = m_networkAccessManager->get(request);

    connect(m_reply, SIGNAL(finished()), this, SLOT(rep()));
    connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
    connect(m_reply, SIGNAL(readyRead()), this, SLOT(rep()));
}

我想向google服务器发送查询并获得响应,但是当我发送请求时,上述任何一个插槽都没有调用,那么我做错了什么?如果我在浏览器中输入该地址,我会得到一些数据。

1 个答案:

答案 0 :(得分:1)

好吧,我认为你在连接中的错误。在调用get()函数之前,必须先声明