Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Qt with Lambda

    General and Desktop
    3
    10
    1388
    Loading More Posts
    • 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.
    • Cobra91151
      Cobra91151 last edited by

      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.

      jsulm Taz742 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Cobra91151 last edited by jsulm

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

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

        Cobra91151 1 Reply Last reply Reply Quote 1
        • Cobra91151
          Cobra91151 @jsulm last edited by

          @jsulm

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

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @Cobra91151 last edited by jsulm

            @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

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

            Cobra91151 1 Reply Last reply Reply Quote 4
            • Cobra91151
              Cobra91151 @jsulm last edited by

              @jsulm

              Ok. I will try it. Thanks.

              1 Reply Last reply Reply Quote 0
              • Cobra91151
                Cobra91151 @jsulm last edited by

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

                1 Reply Last reply Reply Quote 0
                • Taz742
                  Taz742 @Cobra91151 last edited by

                  @Cobra91151 said in Qt with Lambda:

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

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

                  Do what you want.

                  Cobra91151 1 Reply Last reply Reply Quote 0
                  • Cobra91151
                    Cobra91151 @Taz742 last edited by

                    @Taz742

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

                    1 Reply Last reply Reply Quote 0
                    • Cobra91151
                      Cobra91151 last edited by Cobra91151

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

                      jsulm 1 Reply Last reply Reply Quote 0
                      • jsulm
                        jsulm Lifetime Qt Champion @Cobra91151 last edited by

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

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

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post