2013-09-29 5 views
0

저는 C++과 Qt를 처음 사용하기 때문에 자연스럽게 문제가 있습니다. MainWindow와 별도로 URL을 연결하고 JSON 응답을받는 클래스를 만들려고합니다. 그러나 QObject :: connect를 사용하면 프로그램이 SLOT 프로그램에 연결되지 않습니다. QNetworkReply가 트리거되지 않습니다.

다음은 지금까지 (jsonhandle.h) 내 코드입니다 :

#ifndef JSONHANDLE_H 
#define JSONHANDLE_H 

#include <string> 
#include <QObject> 
#include <QJsonObject> 
#include <QtNetwork/QNetworkReply> 

using std::string; 

class jsonhandle: public QObject{ 
    Q_OBJECT 

public: 
    jsonhandle(string url); 
    void sendRequest(); 
    void addArg(string name, string value); 
    void clearArg(); 
    QJsonObject getResponse(); 

public slots: 
    void replyFinished(QNetworkReply * reply); 

private: 
    //////////////////////////////////////////////////////////////////////////////////////////// 
    bool gotResponse;      //Determines whether or not a response was obtained. 
    string url;        //The URL where the JSON request takes place. 
    int size;        //The number of parameters. 
    string *param;       //The array containing arguments. 
    int maxSize;       //Keeps track of the top size of the array. 
    const int DEFAULT = 4;     //The default value of the parameter array. 
    QJsonObject *response;     //The object containing the JSON data. 
    //////////////////////////////////////////////////////////////////////////////////////////// 

    void expandArray();      //Expands the parameter array. 
}; 

#endif // JSONHANDLE_H 

jsonhandle.cpp :

#include "jsonhandle.h" 
#include <string> 
#include <iostream> 
#include <QJsonDocument> 
#include <QJsonObject> 
#include <QString> 
#include <QUrl> 
#include <QUrlQuery> 
#include <QtNetwork/QNetworkAccessManager> 
#include <QtNetwork/QNetworkReply> 
#include <QtNetwork/QNetworkRequest> 
#include <QDebug> 

using std::string; 

jsonhandle::jsonhandle(string url){ 
    //Gets the url and argument list passed to the json handler. 
    this->url = url; 
    this->size = 0; 
    this->maxSize = DEFAULT; 
    this->gotResponse = false; 
    this->response = new QJsonObject(); 

    //Now, creates the paramater array. 
    this->param = new string[DEFAULT]; 
} 

void jsonhandle::addArg(string name, string value){ 
    //Checks to see if the array is full. 
    if (size >= maxSize){ 
     //The array has to be expanded. 
     expandArray(); 
    } 

    //Now, inserts the name and parameter into param. 
    param[size++] = name; 
    param[size++] = value; 
} 

void jsonhandle::expandArray(){ 
    //First gets the size of the array. 
    int newSize = (this->size) * 2; 

    //Creates a new array with double the size. 
    string *newArr = new string[newSize]; 

    //Finally, populates the new array with the parameters. 
    std::copy(param, param + this-> size, newArr); 

    //Finally deletes the original param array. 
    delete[] param; 
    param = newArr; 

    //Finally, updates max count. 
    maxSize *= 2; 
} 

void jsonhandle::clearArg(){ 
    //Clears the counters. 
    this->maxSize = DEFAULT; 
    this->size = 0; 

    //Deletes the param array off the stack. 
    delete[] this->param; 
    this->param = new string[DEFAULT]; 
} 

void jsonhandle::sendRequest(){ 
    //The network manager will post and recieve our HTTP requests. 
    QNetworkAccessManager *manager = new QNetworkAccessManager(this); 

    QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), SLOT(replyFinished(QNetworkReply *))); 

    //The host of the webservice. 
    QString acceptedURL = QString::fromStdString(this->url); 
    QUrl url(acceptedURL); 

    //Sets up the post data request. 
    QNetworkRequest req; 
    req.setUrl(acceptedURL); 
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); 

    //Now, interacts with the data and gets all the additional parameters. 
    QUrlQuery query(url); 
    for (int i = 0; i < size; i += 2){ 
     query.addQueryItem(QString::fromStdString(param[i]), QString::fromStdString(param[i + 1])); 
    } 
    url.setQuery(query); 

    //Update the request with the new query information. 
    req.setUrl(url); 

    // DEBUG TIP: 
    // The following line will allow you print the full HTTP request that will be made 
    // You can enter it directly into a browser, and the JSON object returned will be shown 
    // This is why iostream is included in this file 
    std::cout<< url.toString().toStdString()<<std::endl; 

    //Post the request 
    manager->post(req, url.toEncoded()); 
} 

void jsonhandle::replyFinished(QNetworkReply * reply){ 
    if (reply->error() != QNetworkReply::NoError){ 
     // A communication error has occurred 
     std::cout << "Error in connecting." << std::endl; 
     this->gotResponse = false; 
    } 

    //We read the JSON response into a QJsonObject 
    QJsonObject obj = QJsonDocument::fromJson(reply->readAll()).object(); 
    this->response = &(obj); 
    this->gotResponse = true; 
} 

QJsonObject jsonhandle::getResponse(){ 
    //Sees if a response was obtained from JSON 
    if (gotResponse == false){ 
     response->insert("error",QJsonValue::Null); 
    } 

    return *response; 
} 

너희들은 아무것도 알아낼 경우 알려줘.

+0

어떻게 당신이'replyFinished' 기능이 실행되지 알고 연결하는 더 나은 방법은? 귀하의 프로그램이 아마도 충돌하고 있습니까? – RobbieE

답변

0

QNetworkReply *reply = manager->post (request, data); 
connect (reply, 
    SIGNAL (finished()), 
    this, 
    SLOT (handleReplyFinished())); 
관련 문제