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 #include4 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 #include5 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 137 120 80 9329 10155 11"windowTitle"> 14 <string>MainWindowstring> 15 16class="QWidget" name="centralwidget"> 17 96class="QPushButton" name="pushButton"> 18 30"geometry"> 19 2620 25170 2120 2280 2320 24"text"> 27 <string>获得状态string> 28 29class="QPushButton" name="pushButton_2"> 31 43"geometry"> 32 3933 38170 3460 3580 3620 37"text"> 40 <string>获得外网string> 41 42class="QLabel" name="label"> 44 56"geometry"> 45 5246 5120 4720 4854 4912 50"text"> 53 <string>网络状态:string> 54 55class="QLabel" name="label_2"> 57 69"geometry"> 58 6559 6480 6020 6154 6212 63"text"> 66 <string>无string> 67 68class="QLabel" name="label_3"> 70 82"geometry"> 71 7872 7720 7360 7454 7512 76"text"> 79 <string>互 联 网:string> 80 81class="QLabel" name="label_4"> 83 95"geometry"> 84 9185 9080 8660 8754 8812 89"text"> 92 <string>无string> 93 94class="QMenuBar" name="menubar"> 97 106"geometry"> 98 10599 1040 1000 101329 10222 103class="QStatusBar" name="statusbar"/> 107 108109 110