1

我正在尝试从网页中获取数据,但信号finished()永远不会发出!!!我知道我一定做错了什么,但不知道是什么。

# webservice.h
class WebService:public QObject
{
    Q_OBJECT

 public:
    explicit WebService(QObject *parent=0);
    void getRequest(const QString &urlString);

signals:
    void networkError(QNetworkReply::NetworkError ne);
    void finished(QNetworkReply*);

public slots:
    void parseNetworkResponse(QNetworkReply* finished);

private:
    QNetworkAccessManager *netMgr;

public:
    QByteArray data;
};

#webservice.cpp
WebService::WebService(QObject *parent):QObject(parent)
{
    netMgr = new QNetworkAccessManager;

    connect(netMgr, SIGNAL(finished(QNetworkReply*)),
                 this, SLOT(parseNetworkResponse(QNetworkReply*)));
}

void WebService::getRequest(const QString &urlString)
{
    QUrl url(urlString);
    QNetworkRequest req;
    emit finished(netMgr->get(req));
}

void WebService::parseNetworkResponse(QNetworkReply *finished)
{
    if (finished->error() != QNetworkReply::NoError)
    {
        emit networkError(finished->error());
        return;
    }

    data = finished->readAll();

   qDebug() << data;
}

data永远不会像预期的那样被分配一个值。

4

1 回答 1

1

您没有将 url 传递给QNetworkRequest您创建的。尝试:

QNetworkRequest req(url);

里面WebService::getRequest()

 


main.cpp根据要求,以下是修改后的源代码,以允许它在 QtCreator中作为控制台应用程序项目中的单个文件进行编译和工作:

#include <QCoreApplication>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QByteArray>
#include <QString>

#include <QDebug>

//# webservice.h
class WebService:public QObject
{
    Q_OBJECT

 public:
    explicit WebService(QObject *parent=0);
    void getRequest(const QString &urlString);

signals:
    void networkError(QNetworkReply::NetworkError ne);
    void finished(QNetworkReply*);

public slots:
    void parseNetworkResponse(QNetworkReply* finished);

private:
    QNetworkAccessManager *netMgr;

public:
    QByteArray data;
};

//#webservice.cpp
WebService::WebService(QObject *parent):QObject(parent)
{
    netMgr = new QNetworkAccessManager;

    connect(netMgr, SIGNAL(finished(QNetworkReply*)),
                 this, SLOT(parseNetworkResponse(QNetworkReply*)));
}

void WebService::getRequest(const QString &urlString)
{
    QUrl url(urlString);
    QNetworkRequest req(url);
    emit finished(netMgr->get(req));
}

void WebService::parseNetworkResponse(QNetworkReply *finished)
{
    if (finished->error() != QNetworkReply::NoError)
    {
        qDebug() << "QNetworkReply error: " << finished->error();
        emit networkError(finished->error());
        return;
    }

    data = finished->readAll();

   qDebug() << data;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    WebService web;
    web.getRequest("http://www.google.com");

    return a.exec();
}

#include "main.moc"

所做的微小修改:

  • 添加了必要的标题
  • 添加了一个用适当的 URLmain()调用的WebService::getRequest()
  • 添加#include "main.moc"main.cpp文件的末尾,以便 qmake 将其正确地“moc-ify”为单个、自包含的 .cpp 文件
  • 在问题的答案中进行了上述修复
  • qDebug()在错误情况下添加了一个输出

需要做的最后一件事是QT += network在项目的 .pro 文件中添加,以便将 Qt 网络模块添加到链接步骤和标头搜索路径中。


2013 年 10 月 15 日更新

从您的评论来看,您似乎希望QNetworkAccessManager::get()调用是同步的。我添加了您的示例程序的另一个版本,该版本将在收到WebService::getRequest()请求finished信号之前一直阻塞。请注意,此示例在错误处理方面的表现并不多,如果网络请求未能及时完成,则可能会表现得很糟糕。除了示例或学习代码之外,任何事情都需要适当地处理错误和超时。

此示例中的基本思想是异步 Qt 网络模型中发出的信号由框架的事件循环驱动。因此,当发出请求时,会创建一个新的“嵌套”事件循环,并且WebService::getRequest()函数会执行该循环(并停留在那里),直到finished信号处理程序告诉事件循环退出。

#include <QCoreApplication>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QUrl>

#include <QEventLoop>

#include <QByteArray>
#include <QString>

#include <QDebug>

//# webservice.h


class WebService:public QObject
{
    Q_OBJECT

 public:
    explicit WebService(QObject *parent=0);
    void getRequest(const QString &urlString);

signals:
    void networkError(QNetworkReply::NetworkError ne);
    //void finished(QNetworkReply*);

public slots:
    void parseNetworkResponse(QNetworkReply* finished);

private:
    QNetworkAccessManager *netMgr;
    QEventLoop request_event_loop;

public:
    QByteArray data;
};

//#webservice.cpp
WebService::WebService(QObject *parent):QObject(parent)
{
    netMgr = new QNetworkAccessManager;

    connect(netMgr, SIGNAL(finished(QNetworkReply*)),
                 this, SLOT(parseNetworkResponse(QNetworkReply*)));
}

void WebService::getRequest(const QString &urlString)
{
    QUrl url(urlString);
    QNetworkRequest req(url);
    netMgr->get(req);

    request_event_loop.exec();        // wait here until the WebService::parseNetworkResponse() slot runs
    // emit finished(netMgr->get(req));
}

void WebService::parseNetworkResponse(QNetworkReply *finished)
{
    qDebug() << "enter parseNetworkResponse()";

    if (finished->error() != QNetworkReply::NoError)
    {
        qDebug() << "QNetworkReply error: " << finished->error();
        emit networkError(finished->error());
    }
    else {
        data = finished->readAll();

        qDebug() << data;
    }

    qDebug() << "request_event_loop.exit()";
    request_event_loop.exit();
    qDebug() << "exit parseNetworkResponse()";
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    WebService web;

    qDebug() << "main() getRequest()";
    web.getRequest("http://www.stackoverflow.com");
    qDebug() << "main() getRequest() completed";

    return a.exec();
}

#include "main.moc"
于 2013-10-10T21:13:21.357 回答