Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. lambda slots and QNetworkReply
Qt 6.11 is out! See what's new in the release blog

lambda slots and QNetworkReply

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 541 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    I'm doing something wrong in this code:

    void Packages::download(Package_t package)
    {
        _currentFilename = package.filename;
        _currentFile.setFileName(QString("/mnt/resources/packages/%1").arg(_currentFilename));
        if (!_currentFile.open(QIODevice::WriteOnly))
        {
            qWarning() << "Cannot open" << _currentFile.fileName();
            return;
        }
    
        QUrlQuery query;
        query.addQueryItem("filename", package.filename);
    
        QUrl url(API_HOST + API_DOWNLOAD);
        url.setQuery(query);
    
        QNetworkRequest request(url);
        _managerDownload.setTransferTimeout(10000);
        QNetworkReply *reply = _managerDownload.get(request);
    
        connect(reply, &QNetworkReply::downloadProgress, [reply, this](qint64 bytesReceived, qint64 bytesTotal)
        {
            _currentFile.write(reply->readAll());
            qInfo() << "progress" << bytesReceived << "/" << bytesTotal;
        });
    
        connect(&_managerDownload, &QNetworkAccessManager::finished, [this](QNetworkReply *reply)
        {
            reply->deleteLater();
            int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
            qInfo() << "HTTP status:" << status << reply->url();
            switch (status)
            {
            case 200: qInfo() << "Download completed successfully"; break;
            case 404: qInfo() << "File not found"; break;
            default: break;
            }
    
            _currentFile.close();
            emit downloadFinished(status, _currentFilename);
        });
    
        qInfo() << "GET request" << request.url();
    }
    

    Basically it should:

    1. open a local file where to store the downloaded binary data
    2. create the HTTP GET request
    3. bind the downloadProgress and finished signals to the two lambda functions

    It works, but every time I call this function I see more and more messages, like it does not destroy the previous objects.
    I don't get the mistake because:

    • _managerDownload is a class member, not a pointer
    • reply is deleted inside the finished slot

    Can you help me to understand my fault please?

    J.HilkJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mark81 said in lambda slots and QNetworkReply:

      connect(reply, &QNetworkReply::downloadProgress, [reply, this](qint64 bytesReceived, qint64 bytesTotal)
      {
          _currentFile.write(reply->readAll());
          qInfo() << "progress" << bytesReceived << "/" << bytesTotal;
      });
      

      You should do this once since there's no disconnect because you forgot to specify the receiver's object (third parameter): https://doc.qt.io/qt-5/qobject.html#connect-4 and https://doc.qt.io/qt-5/qobject.html#connect-5 --> set reply as context to make sure the slot gets disconnected or connect only once (which I would prefer)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • M Mark81

        I'm doing something wrong in this code:

        void Packages::download(Package_t package)
        {
            _currentFilename = package.filename;
            _currentFile.setFileName(QString("/mnt/resources/packages/%1").arg(_currentFilename));
            if (!_currentFile.open(QIODevice::WriteOnly))
            {
                qWarning() << "Cannot open" << _currentFile.fileName();
                return;
            }
        
            QUrlQuery query;
            query.addQueryItem("filename", package.filename);
        
            QUrl url(API_HOST + API_DOWNLOAD);
            url.setQuery(query);
        
            QNetworkRequest request(url);
            _managerDownload.setTransferTimeout(10000);
            QNetworkReply *reply = _managerDownload.get(request);
        
            connect(reply, &QNetworkReply::downloadProgress, [reply, this](qint64 bytesReceived, qint64 bytesTotal)
            {
                _currentFile.write(reply->readAll());
                qInfo() << "progress" << bytesReceived << "/" << bytesTotal;
            });
        
            connect(&_managerDownload, &QNetworkAccessManager::finished, [this](QNetworkReply *reply)
            {
                reply->deleteLater();
                int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
                qInfo() << "HTTP status:" << status << reply->url();
                switch (status)
                {
                case 200: qInfo() << "Download completed successfully"; break;
                case 404: qInfo() << "File not found"; break;
                default: break;
                }
        
                _currentFile.close();
                emit downloadFinished(status, _currentFilename);
            });
        
            qInfo() << "GET request" << request.url();
        }
        

        Basically it should:

        1. open a local file where to store the downloaded binary data
        2. create the HTTP GET request
        3. bind the downloadProgress and finished signals to the two lambda functions

        It works, but every time I call this function I see more and more messages, like it does not destroy the previous objects.
        I don't get the mistake because:

        • _managerDownload is a class member, not a pointer
        • reply is deleted inside the finished slot

        Can you help me to understand my fault please?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Mark81 said in lambda slots and QNetworkReply:

        change this

        connect(&_managerDownload, &QNetworkAccessManager::finished, [this](QNetworkReply *reply)

        to

        connect(&_managerDownload, &QNetworkAccessManager::finished, reply, [this](QNetworkReply *reply)

        the reply object in 3rd place should guarantee, that this connect is dissolved once reply is deleted


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        2

        • Login

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