我正在尝试在Qt中实现基本的http连接http://developer.nokia.com/community/wiki/Creating_an_HTTP_network_request_in_Qt,但我很难实现插槽。我是Qt新手。
C:\ Qt5 \ Tools \ QtCreator \ bin \ miniHTTP \ main.cpp:10:错误:请求会员'测试' in' mTest',这是非类型' coreEng()' mTest.Test();
// main.cpp中
#include <QCoreApplication>
#include <coreeng.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
coreEng mTest;
mTest.Test();
return a.exec();
}
// coreeng.h
#ifndef COREENG_H
#define COREENG_H
#include <QDebug>
#include <QObject>
#include <QNetworkAccessManager>
class coreEng : public QObject
{
Q_OBJECT
public:
explicit coreEng(QObject *parent = 0);
void Test();
private slots:
public slots:
void connect();
void url();
void finishedSlot();
private:
QNetworkAccessManager* nam;
};
#endif // COREENG_H
// coreeng.cpp
#include "coreeng.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>
coreEng::coreEng(QNetworkReply* parent) :
QObject(parent)
{
}
void coreEng::Test();
void coreEng::connect(){
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
}
void coreEng::url(){
QUrl url("http://www.forum.nokia.wiki");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}
void coreEng::finishedSlot(QNetworkReply* reply){
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
QImage pic = imageReader.read();
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
}
else
{
}
delete reply;
}
答案 0 :(得分:1)
您已在头文件中声明void Test();
作为成员函数,但尚未在cpp文件中实现它。你应该实现它:
void coreEng::Test()
{
...
}
答案 1 :(得分:1)
除了已经描述的其他问题之外,您完成的插槽的实现与函数定义的签名不同。在您的.h文件中,您有:
void finishedSlot();
而你的.cpp有:
void finishedSlot(QNetworkReply *reply) {
/*code here*/
}
总结一下:
添加测试功能的实现:
void coreEng :: Test(){/ code here /}
修复构造函数的签名,以便.cpp和.h文件一致:
coreEng :: coreEng(QObject * parent):QObject(parent){/ code here /}
修复finishedSlot的签名,以便.cpp和.h文件同意:
void finishedSlot(QNetworkReply * reply);
这些更改至少可以帮助您进入成功编译的阶段。
答案 2 :(得分:0)
一个问题是你遇到了最令人烦恼的解析:http://en.wikipedia.org/wiki/Most_vexing_parse
更改
coreEng mTest();
到
coreEng mTest;
第二个问题是coreeng.cpp中的构造函数具有错误的签名
变化:
coreEng::coreEng(QNetworkReply*) :
QObject(parent)
到
coreEng::coreEng(QObject* parent) :
QObject(parent)