0

i'm trying to implement a simple downloader. but i'm stucked because my reply and its header is empty.

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QtDebug>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/reply/main.qml"));
    viewer.showExpanded();

    QUrl url("http://www.speedtest.qsc.de/10MB.qsc");
    QNetworkRequest request( url );
    request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
    QByteArray range;
    range = "bytes=" + QByteArray::number( 0 ) + "-";
    request.setRawHeader("Range", range );
    QNetworkAccessManager accessManager;
    QNetworkReply* reply = accessManager.get( request );
    qDebug() << __FILE__ << ":" << __LINE__ << reply->size();
    QList<QByteArray> headerFields = reply->rawHeaderList();
    qDebug() << __FILE__ << ":" << __LINE__ << headerFields.count();
    for( qint64 i = 0; 0 < headerFields.count(); i++ ) {
        QString string( headerFields.at(i));
        qDebug() << __FILE__ << ":" << __LINE__ << string;
    }

    return app.exec();
}

what do i have to do, so that my reply isn't empty anymore? thanks in advance!

Kara
  • 6,115
  • 16
  • 50
  • 57
btzs
  • 1,048
  • 3
  • 14
  • 17
  • You need to wait for a reply. [See this question](http://stackoverflow.com/questions/5486090/qnetworkreply-wait-for-finished) – PeterT Oct 29 '13 at 15:55
  • i added QEventLoop loop; connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); below QNetworkReply* reply = accessManager.get( request ); and i get the error: use of undeclared identifier 'connect'. what does this error mean? – btzs Oct 30 '13 at 09:17
  • 1
    people assume that you are usually doing this inside some QT class. Just use `QObject::connect` instead of `connect` – PeterT Oct 30 '13 at 09:46
  • 1
    Oh, not to mention that the `connect` won't work unless the event loop is started. So, you should move your code somewhere after `app.exec()` has been started. – PeterT Oct 30 '13 at 09:57
  • Thanks for the hints. In the real application (not the demo above) i used multiple NetworkAccessManager what caused problems because i didnt knew that they should only be used once per app :-/ :-) – btzs Nov 05 '13 at 09:49

1 Answers1

0

I used multiple QNetworkAccessManager, so that's what caused problems because I didn't know that I should only use one per app.

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
btzs
  • 1,048
  • 3
  • 14
  • 17