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
Forum Update on Monday, May 27th 2025

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.
  • C Offline
    C Offline
    Cobra91151
    wrote on 23 Jan 2018, 09:43 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.

    J T 2 Replies Last reply 23 Jan 2018, 09:50
    0
    • C Cobra91151
      23 Jan 2018, 09:43

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 23 Jan 2018, 09:50 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

      C 1 Reply Last reply 23 Jan 2018, 10:06
      1
      • J jsulm
        23 Jan 2018, 09:50

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

        C Offline
        C Offline
        Cobra91151
        wrote on 23 Jan 2018, 10:06 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.

        J 1 Reply Last reply 23 Jan 2018, 10:10
        0
        • C Cobra91151
          23 Jan 2018, 10:06

          @jsulm

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 23 Jan 2018, 10:10 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

          C 1 Reply Last reply 23 Jan 2018, 10:23
          4
          • J jsulm
            23 Jan 2018, 10:10

            @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

            C Offline
            C Offline
            Cobra91151
            wrote on 23 Jan 2018, 10:23 last edited by
            #5

            @jsulm

            Ok. I will try it. Thanks.

            1 Reply Last reply
            0
            • J jsulm
              23 Jan 2018, 10:10

              @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

              C Offline
              C Offline
              Cobra91151
              wrote on 23 Jan 2018, 12:19 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
              • C Cobra91151
                23 Jan 2018, 09:43

                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.

                T Offline
                T Offline
                Taz742
                wrote on 23 Jan 2018, 12:26 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.

                C 1 Reply Last reply 23 Jan 2018, 12:41
                0
                • T Taz742
                  23 Jan 2018, 12:26

                  @Cobra91151 said in Qt with Lambda:

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

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

                  C Offline
                  C Offline
                  Cobra91151
                  wrote on 23 Jan 2018, 12:41 last edited by
                  #8

                  @Taz742

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

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Cobra91151
                    wrote on 23 Jan 2018, 13:45 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.

                    J 1 Reply Last reply 23 Jan 2018, 14:01
                    0
                    • C Cobra91151
                      23 Jan 2018, 13:45

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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 23 Jan 2018, 14:01 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

                      1/10

                      23 Jan 2018, 09:43

                      • Login

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