Solved QNetworkReply::finished lambda QFile::open switch crash
-
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(); }); }
-
@sonichy Use debugger
-
@sonichy said in QNetworkReply::finished lambda QFile::open switch crash:
fileName
this variable is out of scope due to [&] - C++ basics
-
@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? -
@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.
-
@christian-ehrlicher Fixed !
connect(pReply, &QNetworkReply::finished, [=]() { ...... }
-
@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 -
@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... ;-)