0
votes

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!

1
You need to wait for a reply. See this questionPeterT
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
people assume that you are usually doing this inside some QT class. Just use QObject::connect instead of connectPeterT
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
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

1 Answers

0
votes

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