Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Implementing filedownloader to QML

Implementing filedownloader to QML

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 848 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PsAePr
    wrote on last edited by
    #1

    Hi,

    for my software, i need to download a file to my local HDD. I found this example:
    "Downloading file from URL":http://qt-project.org/forums/viewthread/16749
    and tried to use it. The result looks like this:

    main.cpp
    @
    //main.cpp
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include "filedownloader.h"
    #include <QQmlEngine>
    #include <QtQml>
    #include <QtDebug>
    #include <QObject>

    FileDownloader* n;
    QNetworkAccessManager* nam;
    QObject* parent;

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    QUrl imageURL("http://i.imgur.com/Uw7Fk.jpg");
    viewer.setMainQmlFile(QStringLiteral("qml/DownloadTest4/main.qml"));
    viewer.showExpanded();
    n = new FileDownloader(imageURL, parent);
    //app.connect(n, SIGNAL(downloaded()),SLOT);
    return app.exec();
    }@

    filedownloader.cpp
    @
    //filedownloader.cpp
    #include "filedownloader.h"
    #include <QDebug>
    #include <QFile>

    FileDownloader::FileDownloader(QUrl imageUrl, QObject parent) :
    QObject(parent)
    {
    connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply
    )),
    SLOT(fileDownloaded(QNetworkReply*)));

    QNetworkRequest request(imageUrl);
    m_WebCtrl.get(request);
    qDebug()<<"Request sent!";
    

    }

    FileDownloader::~FileDownloader()
    {

    }

    void FileDownloader::fileDownloaded(QNetworkReply* pReply)
    {
    qDebug()<<"Download successful!";
    m_DownloadedData = pReply->readAll();
    QFile file("D:\Test.jpg");
    file.open(QIODevice::WriteOnly);
    file.write(pReply->readAll());
    //emit a signal
    pReply->deleteLater();
    emit downloaded();

    }

    QByteArray FileDownloader::downloadedData() const
    {
    return m_DownloadedData;
    }@

    filedownloader.h
    @
    //filedownloader.h
    #ifndef FILEDOWNLOADER_H
    #define FILEDOWNLOADER_H

    #include <QObject>
    #include <QByteArray>
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    #include <QNetworkReply>

    class FileDownloader : public QObject
    {
    Q_OBJECT
    public:

    explicit FileDownloader(QUrl imageUrl, QObject *parent = 0);
    
    virtual ~FileDownloader();
    
    QByteArray downloadedData() const;
    

    signals:
    void downloaded();

    private slots:

    void fileDownloaded(QNetworkReply* pReply);
    

    private:

    QNetworkAccessManager m_WebCtrl;
    
    QByteArray m_DownloadedData;
    

    };

    #endif // FILEDOWNLOADER_H@

    Now i have the problem that i get my two console logs "Request sent!" and "Download successful!" but i can't find the file that should have been created.
    Am i doing anything wrong with QFile?

    My second problem is the including of "filedownloader.h" into a QML-file.

    I know that I have to register the C++ as a type, like this:
    @qmlRegisterType<filedownloader>("my.library", 1, 0, "FileDownloader");@
    but it's not working.

    I hope somebody can help me :)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stevenceuppens
      wrote on last edited by
      #2

      Hi,

      in your example code you do:

      @void FileDownloader::fileDownloaded(QNetworkReply* pReply)
      {
      qDebug()<<"Download successful!";
      m_DownloadedData = pReply->readAll();
      QFile file("D:\Test.jpg");
      file.open(QIODevice::WriteOnly);
      file.write(pReply->readAll());
      //emit a signal
      pReply->deleteLater();
      emit downloaded();
      }
      @

      the line "m_DownloadedData = pReply->readAll();" creates this issue,

      after this line, the buffer of your network reply is empty, and the data is inside "m_DownloadedData"

      a few lines lower, you try to save the image by doing: "file.write(pReply->readAll());"

      but the buffer is already empty...

      so you shoud do: "file.write( m_DownloadedData );"

      i should do,

      @
      void FileDownloader::fileDownloaded(QNetworkReply* pReply)
      {
      qDebug()<<"Download successful!";
      QFile file("D:\Test.jpg");

      if( !file.open(QIODevice::WriteOnly) )
          return;  // file could not be created
      
      file.write(pReply->readAll());
      //emit a signal
      pReply->deleteLater();
      emit downloaded();
      

      }
      @

      and remove the variable "m_DownloadedData"

      for your second issue,

      you also have to include the macro QML_DECLARE_TYPE(CLASSNAME)
      in your header file, before you can use the qmlRegisterType method.

      so:

      @
      //filedownloader.h
      #ifndef FILEDOWNLOADER_H
      #define FILEDOWNLOADER_H

      #include <QtQml> // add for QML_DECLARE_TYPE macro !!!!!

      #include <QObject>
      #include <QByteArray>
      #include <QNetworkAccessManager>
      #include <QNetworkRequest>
      #include <QNetworkReply>

      class FileDownloader : public QObject
      {
      Q_OBJECT
      public:

      explicit FileDownloader(QUrl imageUrl, QObject *parent = 0);
      
      virtual ~FileDownloader();
      
      QByteArray downloadedData() const;
      

      signals:
      void downloaded();

      private slots:

      void fileDownloaded(QNetworkReply* pReply);
      

      private:

      QNetworkAccessManager m_WebCtrl;
      
      QByteArray m_DownloadedData;
      

      };

      QML_DECLARE_TYPE( FileDownloader ) // marco !!

      #endif // FILEDOWNLOADER_H
      @

      Steven CEUPPENS
      Developer &#x2F; Architect
      Mobile: +32 479 65 93 10

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved