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.


  • Moderators

    @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.



  • @jsulm

    So what is the better solution to check network fast? For example, I have also method using QTcpSocket but it takes very long.


  • Moderators

    @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



  • @jsulm

    Ok. I will try it. Thanks.



  • @jsulm

    I 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.



  • @Cobra91151 said in Qt with Lambda:

    [this, netCheck](QNetworkReply *netReply) mutable -> bool {

    I thought you forgot & in netCheck? try &netCheck.....



  • @Taz742

    I have changed to &netCheck. But it still return false.



  • I think I will use QTcpSocket solution. I have remove 5000 msec parameter form waitForConnected and it works faster. Thanks.


  • Moderators

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.