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
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 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 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 #include <QNetworkConfigurationManager>
 7 #include <QHostInfo>
 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
View Code

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 }
View Code

mainwindow.ui

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

 

posted on 2022-02-09 10:03  疯狂delphi  阅读(601)  评论(0编辑  收藏  举报

导航