Qt World Summit: Register Today!

Simple Http Get

  • Hello guys, I have a problem. I try get simple .txt content from my localhost "http://localhost/api/v1.txt" but I don't receive any signal from my connections.

    //your code here
    #ifndef REQUESTS_HPP
    #define REQUESTS_HPP
    #include <QObject>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QNetworkRequest>
    #include <QUrl>
    #include <QUrlQuery>
    class Requests : public QObject
        explicit Requests(QObject *parent = 0);
            delete m_reply;
        void    setUrlServer(const QString& s) { urlServer = s; }
        void    setPostQueryData(const QString& key, const QString& value);
        void    requestByGet();
    public slots:
        void    replyFinished(QNetworkReply* rply);
        void    rGet_read();
        void    rGet_end();
        void    rErrors(QNetworkReply::NetworkError errorT);
        void    rMetaChange();
        void    rRedictActivated(QUrl url);
        void    rSSlErrorsDetected(QList<QSslError> errors);
        QUrlQuery       hpost_dataQuery;
        QUrl            urlServer;
        // -- NETWORK
        QNetworkAccessManager   m_manager;
        QNetworkReply           *m_reply;
    #endif // REQUESTS_HPP


    //your code here
    #include "requests.hpp"
    Requests::Requests(QObject *parent) : QObject(parent)
        m_reply = nullptr;
        //connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    void Requests::setPostQueryData(const QString &key, const QString &value)
        hpost_dataQuery.addQueryItem(key, value);
    void Requests::requestByGet()
        QNetworkRequest __request;
        m_reply = m_manager.get(__request);
        qDebug() << "getting from url: " << urlServer.toString();
        connect(m_reply, SIGNAL(readyRead()), SLOT(rGet_read()));
        connect(m_reply, SIGNAL(finished()), SLOT(rGet_end()));
        connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(rErrors(QNetworkReply::NetworkError)));
        connect(m_reply, SIGNAL(metaDataChanged()), SLOT(rMetaChange()));
        connect(m_reply, SIGNAL(redirected(QUrl)), SLOT(rRedictActivated(QUrl)));
        connect(m_reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(rSSlErrorsDetected(QList<QSslError>)));
    void Requests::replyFinished(QNetworkReply *rply)
        if(rply->error() == QNetworkReply::NoError)
            int http_statusCode = rply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toUInt();
            if(http_statusCode >= 200 && http_statusCode < 300)
                    QString ReplyString = QString::fromUtf8(rply->readAll());
    #ifdef _DEBUG
                    qDebug() << ReplyString;
            else if(http_statusCode >= 300 && http_statusCode < 400)
                // Get the redirection url
                QUrl NewUrl = rply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
                // Because the redirection url can be relative, we have to use
                // prevous one to resolve it
                NewUrl = rply->url().resolved(NewUrl);
                QNetworkAccessManager *tManager = rply->manager();
                QNetworkRequest tRedirection(NewUrl);
                return; // to keep the manager for the next request
                qDebug() << "Error on reply finished.";
    void Requests::rGet_read()
        qDebug() << "rr";
    void Requests::rGet_end()
        qDebug() << "fim\n";
    void Requests::rErrors(QNetworkReply::NetworkError errorT)
        qDebug() << errorT;
    void Requests::rMetaChange()
        qDebug() << "Meta changed...";
    void Requests::rRedictActivated(QUrl url)
        qDebug() << "Received redirect: " << url.toString();
    void Requests::rSSlErrorsDetected(QList<QSslError> errors)
        qDebug() << "Received ssl errors";

    Someone have any idea why i don't receive any signal?

  • You are missing the receiver argument in connects.
    connect(m_reply, SIGNAL(readyRead()), SLOT(rGet_read())); should become connect(m_reply, SIGNAL(readyRead()), this,SLOT(rGet_read()));
    if you used the new connect syntax it would detect this kind of things at compile time

  • Unfortunaly no, i changed my connections and not receive any signal.

    //your code here
    connect(m_reply, SIGNAL(readyRead()), this, SLOT(rGet_read()));
        connect(m_reply, SIGNAL(finished()), this, SLOT(rGet_end()));

  • does connect return true?

Log in to reply