Qt-获得网络状态(testNetOnIine)(Qt5.14.2+win10)


相关资料:

https://blog.csdn.net/qq_43658314/article/details/109814071   QT学习记录 --- 利用QNetworkConfigurationManager检测网络连接变化

https://www.cnblogs.com/itrena/p/5938377.html      QHostInfo类为主机名查找提供了静态函数。

https://www.cnblogs.com/liushui-sky/articles/9474478.html     Qt判断网络连接

实例代码:

testNetOnIine.pro

 1 QT       += core gui
 2 QT       += network
 3 
 4 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 5 
 6 CONFIG += c++11
 7 
 8 # The following define makes your compiler emit warnings if you use
 9 # any Qt feature that has been marked deprecated (the exact warnings
10 # depend on your compiler). Please consult the documentation of the
11 # deprecated API in order to know how to port your code away from it.
12 DEFINES += QT_DEPRECATED_WARNINGS
13 
14 # You can also make your code fail to compile if it uses deprecated APIs.
15 # In order to do so, uncomment the following line.
16 # You can also select to disable deprecated APIs only up to a certain version of Qt.
17 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
18 
19 SOURCES += \
20     main.cpp \
21     mainwindow.cpp
22 
23 HEADERS += \
24     mainwindow.h
25 
26 FORMS += \
27     mainwindow.ui
28 
29 # Default rules for deployment.
30 qnx: target.path = /tmp/$${TARGET}/bin
31 else: unix:!android: target.path = /opt/$${TARGET}/bin
32 !isEmpty(target.path): INSTALLS += target

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include 
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include 
 5 
 6 #include 
 7 #include 
 8 
 9 QT_BEGIN_NAMESPACE
10 namespace Ui { class MainWindow; }
11 QT_END_NAMESPACE
12 
13 class MainWindow : public QMainWindow
14 {
15     Q_OBJECT
16 
17 public:
18     MainWindow(QWidget *parent = nullptr);
19     ~MainWindow();
20 
21 private slots:
22     void on_pushButton_clicked();
23 
24     void on_pushButton_2_clicked();
25     void onLookupHost(QHostInfo host);
26 
27 private:
28     Ui::MainWindow *ui;
29 };
30 #endif // MAINWINDOW_H

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9 
10     setWindowTitle(QStringLiteral("获得网络状态"));
11 }
12 
13 MainWindow::~MainWindow()
14 {
15     delete ui;
16 }
17 
18 void MainWindow::on_pushButton_clicked()
19 {
20     QNetworkConfigurationManager mgr;
21     if (mgr.isOnline())
22     {
23         ui->label_2->setText(QStringLiteral("有连接"));
24     }
25     else
26     {
27         ui->label_2->setText(QStringLiteral("无连接"));
28     }
29 }
30 
31 void MainWindow::on_pushButton_2_clicked()
32 {
33     QHostInfo::lookupHost("www.baidu.com", this, SLOT(onLookupHost(QHostInfo)));
34 }
35 
36 void MainWindow::onLookupHost(QHostInfo host)
37 {
38     if (host.error() != QHostInfo::NoError)
39     {
40         qDebug() << "Lookup failed:" << host.errorString();
41         ui->label_4->setText(QStringLiteral("无外网"));
42     }
43     else{
44         ui->label_4->setText(QStringLiteral("有外网"));
45     }
46 }

mainwindow.ui

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 "4.0">
  3  <class>MainWindowclass>
  4  class="QMainWindow" name="MainWindow">
  5   "geometry">
  6    
  7     0
  8     0
  9     329
 10     155
 11    
 12   
 13   "windowTitle">
 14    <string>MainWindowstring>
 15   
 16   class="QWidget" name="centralwidget">
 17    class="QPushButton" name="pushButton">
 18     "geometry">
 19      
 20       170
 21       20
 22       80
 23       20
 24      
 25     
 26     "text">
 27      <string>获得状态string>
 28     
 29    
 30    class="QPushButton" name="pushButton_2">
 31     "geometry">
 32      
 33       170
 34       60
 35       80
 36       20
 37      
 38     
 39     "text">
 40      <string>获得外网string>
 41     
 42    
 43    class="QLabel" name="label">
 44     "geometry">
 45      
 46       20
 47       20
 48       54
 49       12
 50      
 51     
 52     "text">
 53      <string>网络状态:string>
 54     
 55    
 56    class="QLabel" name="label_2">
 57     "geometry">
 58      
 59       80
 60       20
 61       54
 62       12
 63      
 64     
 65     "text">
 66      <string>无string>
 67     
 68    
 69    class="QLabel" name="label_3">
 70     "geometry">
 71      
 72       20
 73       60
 74       54
 75       12
 76      
 77     
 78     "text">
 79      <string>互 联 网:string>
 80     
 81    
 82    class="QLabel" name="label_4">
 83     "geometry">
 84      
 85       80
 86       60
 87       54
 88       12
 89      
 90     
 91     "text">
 92      <string>无string>
 93     
 94    
 95   
 96   class="QMenuBar" name="menubar">
 97    "geometry">
 98     
 99      0
100      0
101      329
102      22
103     
104    
105   
106   class="QStatusBar" name="statusbar"/>
107  
108  
109  
110 

相关