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. Qt with Lambda

Qt with Lambda

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.9k 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by
    #1

    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.

    jsulmJ Taz742T 2 Replies Last reply
    0
    • Cobra91151C Cobra91151

      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.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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

      Cobra91151C 1 Reply Last reply
      1
      • jsulmJ 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.

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #3

        @jsulm

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

        jsulmJ 1 Reply Last reply
        0
        • Cobra91151C Cobra91151

          @jsulm

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

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @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

          Cobra91151C 1 Reply Last reply
          4
          • jsulmJ 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

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #5

            @jsulm

            Ok. I will try it. Thanks.

            1 Reply Last reply
            0
            • jsulmJ 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

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by
              #6

              @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
              0
              • Cobra91151C Cobra91151

                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.

                Taz742T Offline
                Taz742T Offline
                Taz742
                wrote on last edited by
                #7

                @Cobra91151 said in Qt with Lambda:

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

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

                Do what you want.

                Cobra91151C 1 Reply Last reply
                0
                • Taz742T Taz742

                  @Cobra91151 said in Qt with Lambda:

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

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

                  Cobra91151C Offline
                  Cobra91151C Offline
                  Cobra91151
                  wrote on last edited by
                  #8

                  @Taz742

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

                  1 Reply Last reply
                  0
                  • Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by Cobra91151
                    #9

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

                    jsulmJ 1 Reply Last reply
                    0
                    • Cobra91151C Cobra91151

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

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved