0

您好我正在尝试发送 HTTP GET 请求并使用函数接收结果。但我收到此错误:

调用 'HttpNetworkRequest::connect(QNetworkAccessManager*&, const char*, RequestFinishFunction&, const char*)' 没有匹配的函数

连接函数在这个 HttpNetworkRequest.cpp 文件中

#include "HttpNetworkRequest.hpp"
#include <QObject>
#include <QIODevice>
#include <QDir>

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/XmlDataModel>
#include <RequestFinishFunction.hpp>
using namespace bb::cascades;

HttpNetworkRequest::HttpNetworkRequest(bb::cascades::Application *app)
: QObject(app)
{
QNetworkRequest networkRequest = QNetworkRequest();
networkRequest.setUrl(QUrl("http://www.blackberry.com"));
RequestFinishFunction req;
QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager;
bool res =        connect(networkAccessManager,SIGNAL(finished(QNetworkReply*)),
    req,SLOT(requestFinished()));
Q_ASSERT(res);
Q_UNUSED(res);
networkAccessManager->get(networkRequest);
}

包含的 RequestFinishFunction.hpp

#ifndef REQUESTFINISHFUNCTION_HPP_
#define REQUESTFINISHFUNCTION_HPP_
#include <bb/cascades/ActivityIndicator>
#include <QObject>
#include <QObject>
class RequestFinishFunction : QObject
{
public slots:
void requestFinished(QNetworkReply* reply);
};
#endif /* REQUESTFINISHFUNCTION_HPP_ */

RequestFinishFunction.cpp 文件是

#include <bb/cascades/ActivityIndicator>
#include <QObject>
#include <QObject>
#include <RequestFinishFunction.hpp>
void RequestFinishFunction::requestFinished(QNetworkReply* reply)
{
    reply->deleteLater();
}
4

1 回答 1

2

连接方法的语法

bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection )

我们必须传递发送者和接收者的引用。

您没有发送接收者的参考。您有两种选择。

1) 在 connect 方法调用中使用 & 运算符发送 req 的引用

代替

连接(networkAccessManager,SIGNAL(完成(QNetworkReply *)),req,SLOT(requestFinished()));

连接(networkAccessManager,SIGNAL(完成(QNetworkReply*)), &req,SLOT(requestFinished()));

或者

2) 将 req 声明为指针

代替

RequestFinishFunction 请求;

请求完成函数 *req;

于 2012-12-18T08:29:27.370 回答