Qt with Lambda
-
Hi! I want to check network connection.
bool Test::checkNetConnection() { bool netCheck = false; QNetworkAccessManager *netManager = new QNetworkAccessManager(this); netManager->get(QNetworkRequest(QUrl("https://google.com"))); connect(netManager, &QNetworkAccessManager::finished, [this, netCheck](QNetworkReply *netReply) mutable -> bool { if (netReply->bytesAvailable()) { netCheck = true; } else { netCheck = false; } netReply->close(); netReply->deleteLater(); return netCheck; }); return netCheck; }
The problem is, it's always return false. I think the problem is with lambda expression. Any ideas? Thanks.
-
Hi! I want to check network connection.
bool Test::checkNetConnection() { bool netCheck = false; QNetworkAccessManager *netManager = new QNetworkAccessManager(this); netManager->get(QNetworkRequest(QUrl("https://google.com"))); connect(netManager, &QNetworkAccessManager::finished, [this, netCheck](QNetworkReply *netReply) mutable -> bool { if (netReply->bytesAvailable()) { netCheck = true; } else { netCheck = false; } netReply->close(); netReply->deleteLater(); return netCheck; }); return netCheck; }
The problem is, it's always return false. I think the problem is with lambda expression. Any ideas? Thanks.
@Cobra91151 No, the problem is: you start the request and then do
return netCheck;
in Test::checkNetConnection().
At that time your lambda wasn't called yet and did not change the value of netCheck.
If you want Test::checkNetConnection() to return the status you would need to wait inside the method until the result is available. But this would block the thread. -
@Cobra91151 No, the problem is: you start the request and then do
return netCheck;
in Test::checkNetConnection().
At that time your lambda wasn't called yet and did not change the value of netCheck.
If you want Test::checkNetConnection() to return the status you would need to wait inside the method until the result is available. But this would block the thread.So what is the better solution to check network fast? For example, I have also method using
QTcpSocket
but it takes very long. -
So what is the better solution to check network fast? For example, I have also method using
QTcpSocket
but it takes very long.@Cobra91151 Why not simply emit a signal with the network status when it changes? You can emit this signal inside your lambda and everyone interested in this information can connect to this signal.
You can use http://doc.qt.io/qt-5/qnetworkaccessmanager.html#networkAccessibleChanged -
@Cobra91151 Why not simply emit a signal with the network status when it changes? You can emit this signal inside your lambda and everyone interested in this information can connect to this signal.
You can use http://doc.qt.io/qt-5/qnetworkaccessmanager.html#networkAccessibleChangedOk. I will try it. Thanks.
-
@Cobra91151 Why not simply emit a signal with the network status when it changes? You can emit this signal inside your lambda and everyone interested in this information can connect to this signal.
You can use http://doc.qt.io/qt-5/qnetworkaccessmanager.html#networkAccessibleChangedI have checked it but it doesn't emits anything.
Code:
connect(this, &Test::networkData, this, &Test::setNetworkData); checkNetConnection();
void Test::checkNetConnection() { QNetworkAccessManager *netManager = new QNetworkAccessManager(this); netManager->get(QNetworkRequest(QUrl("https://google.com"))); connect(netManager, &QNetworkAccessManager::networkAccessibleChanged, [this](QNetworkAccessManager::NetworkAccessibility accessible) { emit networkData(accessible); }); } void Test::setNetworkData(QNetworkAccessManager::NetworkAccessibility accessible) { switch (accessible) { case QNetworkAccessManager::Accessible: qDebug() << "Access"; break; case QNetworkAccessManager::NotAccessible: qDebug() << "No access"; break; case QNetworkAccessManager::UnknownAccessibility: qDebug() << "UnknownAccessibility"; break; default: qDebug() << "Error"; } }
Any ideas? Thanks.
-
Hi! I want to check network connection.
bool Test::checkNetConnection() { bool netCheck = false; QNetworkAccessManager *netManager = new QNetworkAccessManager(this); netManager->get(QNetworkRequest(QUrl("https://google.com"))); connect(netManager, &QNetworkAccessManager::finished, [this, netCheck](QNetworkReply *netReply) mutable -> bool { if (netReply->bytesAvailable()) { netCheck = true; } else { netCheck = false; } netReply->close(); netReply->deleteLater(); return netCheck; }); return netCheck; }
The problem is, it's always return false. I think the problem is with lambda expression. Any ideas? Thanks.
@Cobra91151 said in Qt with Lambda:
[this, netCheck](QNetworkReply *netReply) mutable -> bool {
I thought you forgot & in netCheck? try &netCheck.....
-
@Cobra91151 said in Qt with Lambda:
[this, netCheck](QNetworkReply *netReply) mutable -> bool {
I thought you forgot & in netCheck? try &netCheck.....
I have changed to
&netCheck
. But it still return false. -
I think I will use
QTcpSocket
solution. I have remove 5000 msec parameter formwaitForConnected
and it works faster. Thanks. -
I think I will use
QTcpSocket
solution. I have remove 5000 msec parameter formwaitForConnected
and it works faster. Thanks.@Cobra91151 Well, networkAccessibleChanged() will only be emitted when the network status changes. You can simply try to disable your network connection to trigger the signal.