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. QNetworkReply::finished lambda QFile::open switch crash
Forum Updated to NodeBB v4.3 + New Features

QNetworkReply::finished lambda QFile::open switch crash

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 759 Views
  • 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by sonichy
    #1

    替代文字

    void FtpManager::get(const QString &path, const QString &fileName)
    {
        m_pUrl.setPath(path);
        QNetworkReply *pReply = m_manager.get(QNetworkRequest(m_pUrl));
        //connect(pReply, SIGNAL(finished()), this, SLOT(finished()));
        connect(pReply, &QNetworkReply::finished, [&, pReply]() {
            qDebug() << pReply->errorString();
            switch (pReply->error()) {
            case QNetworkReply::NoError :{
                QFile m_file;
                m_file.setFileName(fileName);
                m_file.open(QIODevice::WriteOnly);
                m_file.write(pReply->readAll());
                m_file.flush();
                m_file.close();
                break;}
            default:
                break;
            }
            pReply->deleteLater();
        });
    }
    

    https://github.com/sonichy

    jsulmJ 1 Reply Last reply
    0
    • sonichyS sonichy

      替代文字

      void FtpManager::get(const QString &path, const QString &fileName)
      {
          m_pUrl.setPath(path);
          QNetworkReply *pReply = m_manager.get(QNetworkRequest(m_pUrl));
          //connect(pReply, SIGNAL(finished()), this, SLOT(finished()));
          connect(pReply, &QNetworkReply::finished, [&, pReply]() {
              qDebug() << pReply->errorString();
              switch (pReply->error()) {
              case QNetworkReply::NoError :{
                  QFile m_file;
                  m_file.setFileName(fileName);
                  m_file.open(QIODevice::WriteOnly);
                  m_file.write(pReply->readAll());
                  m_file.flush();
                  m_file.close();
                  break;}
              default:
                  break;
              }
              pReply->deleteLater();
          });
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @sonichy Use debugger

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @sonichy said in QNetworkReply::finished lambda QFile::open switch crash:

        fileName

        this variable is out of scope due to [&] - C++ basics

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

        JonBJ 1 Reply Last reply
        4
        • Christian EhrlicherC Christian Ehrlicher

          @sonichy said in QNetworkReply::finished lambda QFile::open switch crash:

          fileName

          this variable is out of scope due to [&] - C++ basics

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @christian-ehrlicher
          I presume you are talking about the & in [&, pReply](). For those of us unaware of this C++ lambda syntax, could you point to a reference explaining, please?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @JonB: @J-Hilk explained it some hours ago here: https://forum.qt.io/topic/106751/connecting-to-statusbar-showmessage-gives-compile-time-error-in-new-syntax/11 and searching for c++11 lambda will show you e.g. https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture which should be read before someone is using a lambda.

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

            sonichyS 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @JonB: @J-Hilk explained it some hours ago here: https://forum.qt.io/topic/106751/connecting-to-statusbar-showmessage-gives-compile-time-error-in-new-syntax/11 and searching for c++11 lambda will show you e.g. https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture which should be read before someone is using a lambda.

              sonichyS Offline
              sonichyS Offline
              sonichy
              wrote on last edited by sonichy
              #6

              @christian-ehrlicher Fixed !

              connect(pReply, &QNetworkReply::finished, [=]() {
                    ......
              }
              

              https://github.com/sonichy

              J.HilkJ 1 Reply Last reply
              0
              • sonichyS sonichy

                @christian-ehrlicher Fixed !

                connect(pReply, &QNetworkReply::finished, [=]() {
                      ......
                }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

                @sonichy did you read the thread or the article, that @Christian-Ehrlicher linked?

                From either you should be able to deduce, how to capture filename by value instead of reference


                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.

                JonBJ 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @sonichy did you read the thread or the article, that @Christian-Ehrlicher linked?

                  From either you should be able to deduce, how to capture filename by value instead of reference

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @j-hilk , @Christian-Ehrlicher
                  No, I didn't spot that link somehow, now I do, and I see it explains. So that's what I was looking for. I shall add this new & to the = for the (tortured) list of new "overloaded operator symbols with different meanings" that C++ wants me to remember... ;-)

                  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