Important: Please read the Qt Code of Conduct -

How to use qml to download a file from http

  • I am new to QT.
    I want to click a button in qml ,then download the file from http! I use below code in DownLoadFile.h
    class CDownLoadFile: public QObject
    explicit CDownLoadFile(QObject *parent=0);


    public slots:
    Q_INVOKABLE void downloadFile(QUrl url);
    Q_INVOKABLE void fileIsReady( QNetworkReply * reply);
    QUrl url;
    QNetworkAccessManager manager;
    QNetworkReply *preply;
    QFile *file;
    int httpGetId;

    #include "DownLoadFile.h"
    #include <QtGui>
    #include <QtNetwork>

    CDownLoadFile::CDownLoadFile(QObject *parent)
    manager=new QNetworkAccessManager(this);

    void CDownLoadFile::downloadFile(QUrl url)
    connect(manager, SIGNAL(finished()), this, SLOT(fileIsReady(QNetworkReply*)));
    preply= manager->get(QNetworkRequest(url);

    void CDownLoadFile::fileIsReady( QNetworkReply * preply)
    QTemporaryFile temp_file;

    but when compile ,these probelm come ,I am not sure what is wrong!
    ../Reader/DownLoadFile.cpp: In constructor ‘CDownLoadFile::CDownLoadFile(QObject*)’:
    ../Reader/DownLoadFile.cpp:8: error: no match for ‘operator=’ in ‘((CDownLoadFile*)this)->CDownLoadFile::manager = (operator new(8u), (<statement>, ((QNetworkAccessManager*)<anonymous>)))’
    ../../NokiaQtSdk/Desktop/Qt/473/gcc/include/QtNetwork/qnetworkaccessmanager.h:72: note: candidates are: QNetworkAccessManager& QNetworkAccessManager::operator=(const QNetworkAccessManager&)
    ../Reader/DownLoadFile.cpp: In member function ‘void CDownLoadFile::downloadFile(QUrl)’:
    ../Reader/DownLoadFile.cpp:13: error: no matching function for call to ‘CDownLoadFile::connect(QNetworkAccessManager&, const char [12], CDownLoadFile* const, const char [29])’
    ../../NokiaQtSdk/Desktop/Qt/473/gcc/include/QtCore/qobject.h:198: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
    ../../NokiaQtSdk/Desktop/Qt/473/gcc/include/QtCore/qobject.h:313: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
    ../Reader/DownLoadFile.cpp:14: error: base operand of ‘->’ has non-pointer type ‘QNetworkAccessManager’
    ../Reader/DownLoadFile.cpp:14: error: expected ‘)’ before ‘;’ token
    make: *** [DownLoadFile.o] Error 1
    make: Leaving directory `/home/administrator/qt/Reader-build-desktop'

    Thank you !
    My regards!

  • The first problem I see is that
    @QNetworkAccessManager manager;@
    this should be
    @QNetworkAccessManager *manager;@

  • The wiki contains an example C++ code which shows "how to download content from HTTP":

Log in to reply