简体   繁体   English

QNetworkAccessManager出现插槽错误

[英]slot error using QNetworkAccessManager

I am trying to implement a basic http connection http://developer.nokia.com/community/wiki/Creating_an_HTTP_network_request_in_Qt into Qt but I have difficultly implementing the slot. 我正在尝试将基本的http连接http://developer.nokia.com/community/wiki/Creating_an_HTTP_network_request_in_Qt实现到Qt中,但是我很难实现该插槽。 I am a Qt novice. 我是Qt新手。

C:\\Qt5\\Tools\\QtCreator\\bin\\miniHTTP\\main.cpp:10: error: request for member 'Test' in 'mTest', which is of non-class type 'coreEng()' mTest.Test(); C:\\ Qt5 \\ Tools \\ QtCreator \\ bin \\ miniHTTP \\ main.cpp:10:错误:请求'mTest'中的成员'Test',该成员是非类类型的'coreEng()'mTest.Test();

//main.cpp //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 //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 //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;

}

You have declared void Test(); 您已声明void Test(); as a member function in the header file and have not implemented it in the cpp file. 作为头文件中的成员函数,而尚未在cpp文件中实现。 You should implement it : 您应该实现它:

void coreEng::Test()
{
    ...
}

In addition to other problems already described, your implementation of finished slot doesn't have the same signature of as the function definition. 除了已经描述的其他问题之外,您完成的插槽实现与函数定义的签名不同。 In your .h file you have: 在您的.h文件中,您具有:

void finishedSlot();

Whereas your .cpp has: 而您的.cpp具有:

void finishedSlot(QNetworkReply *reply) {
    /*code here*/
}

So to summarise: 总结一下:

  1. Add an implementation of your test function: 添加测试功能的实现:

    void coreEng::Test() { / code here / } 无效coreEng :: Test(){/ 代码在这里 /}

  2. Fix the signature of your constructor so that .cpp and .h files agree: 修复构造函数的签名,以使.cpp和.h文件一致:

    coreEng::coreEng(QObject* parent) : QObject(parent) { / code here / } coreEng :: coreEng(QObject * parent):QObject(parent){/ 代码在这里 /}

  3. Fix the signature of your finishedSlot so that .cpp and .h files agree: 修复您的finishSlot的签名,以使.cpp和.h文件一致:

    void finishedSlot(QNetworkReply *reply); 无效finishSlot(QNetworkReply * reply);

These changes will at least get you to the stage that you're compiling successfully. 这些更改至少将使您进入编译成功的阶段。

One problem is you are running into the most vexing parse: http://en.wikipedia.org/wiki/Most_vexing_parse 一个问题是您遇到了最烦人的解析: http : //en.wikipedia.org/wiki/Most_vexing_parse

Most vexing parse 最烦人的解析

Change 更改

coreEng mTest();

to

coreEng mTest;

A second problem is your constructor in coreeng.cpp has the wrong signature 第二个问题是您在coreeng.cpp中的构造函数签名错误

Change: 更改:

coreEng::coreEng(QNetworkReply*) :
    QObject(parent)

to

   coreEng::coreEng(QObject* parent) :
        QObject(parent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM