Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How do you know if it is connected?
Forum Update on Monday, May 27th 2025

How do you know if it is connected?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 363 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.
  • F Offline
    F Offline
    fanxinglanyu
    wrote on last edited by
    #1

    This is my code.But I have a problem. When the Internet is disconnected, it will show that the Internet is not disconnected for 1 minute.Thanks,I need your help.

        QHostInfo::lookupHost("www.baidu.com", this, &MainWindow::netWorkStatus);
    bool MainWindow::netWorkStatus(const QHostInfo& host)
    {
    
        if (host.error() != QHostInfo::NoError) {
            qDebug() << "Lookup failed:" << host.errorString();
            ui->label_2->setText("Broken network");
            return false;
        }else{
    
            qDebug()<<"";
            ui->label_2->setText("Connected to the Internet");
    
           // QHostInfo::abortHostLookup(lookupID);
    
            return true;
        }
    }
    
    raven-worxR 1 Reply Last reply
    0
    • F fanxinglanyu

      This is my code.But I have a problem. When the Internet is disconnected, it will show that the Internet is not disconnected for 1 minute.Thanks,I need your help.

          QHostInfo::lookupHost("www.baidu.com", this, &MainWindow::netWorkStatus);
      bool MainWindow::netWorkStatus(const QHostInfo& host)
      {
      
          if (host.error() != QHostInfo::NoError) {
              qDebug() << "Lookup failed:" << host.errorString();
              ui->label_2->setText("Broken network");
              return false;
          }else{
      
              qDebug()<<"";
              ui->label_2->setText("Connected to the Internet");
      
             // QHostInfo::abortHostLookup(lookupID);
      
              return true;
          }
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @fanxinglanyu
      https://forum.qt.io/topic/98676/how-to-check-whether-the-computer-connects-the-internet

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • F Offline
        F Offline
        fanxinglanyu
        wrote on last edited by
        #3

        Firstly, thanks your reply!However,I think this isn't solve my question.In China, Google is not accessible.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fanxinglanyu
          wrote on last edited by
          #4

          I use a method to solve this problem.This is my code:

          bool MainWindow::check()
          {
              QTcpSocket* sock = new QTcpSocket(this);
              sock->connectToHost("www.google.com", 80);
              bool connected = sock->waitForConnected(30000);//ms
          
              if (!connected)
              {
                  sock->abort();
                  return false;
              }
              sock->close();
              return true;
          }
          
          1 Reply Last reply
          0
          • F Offline
            F Offline
            fanxinglanyu
            wrote on last edited by
            #5

            I think I have a better idea of using threads to monitor network state. The way to do this is to use the above method in the thread and use QTimer.

            1 Reply Last reply
            0

            • Login

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