【VS+QT开发】获取本地网络信息小软件(C++)
生活随笔
收集整理的這篇文章主要介紹了
【VS+QT开发】获取本地网络信息小软件(C++)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡介
考慮到之前的那一個安裝可能實現上有點復雜,也不知道你愿不愿意看。
所以,這里就實現一個簡單的。
對了,考慮到我垃圾般的程序員審美,所以,如果覺得不好看的話,后期可以自己嘗試看看能不能挑一下顏色,圖片等一系列操作的。
- 但是,下面的這版本,就是全用代碼實現的。
- 原因很簡單,就是我擔心你不太會用Qt designer
- 但是,其實這個還是蠻簡單的。就是簡單大拖動一下就好。只需要記住這個ui的這個控件,叫什么名字,自己后期針對性的控制一下就好了。
開發環境:
- VS2017 + QT
- 關于上面的這個東西怎么配置可以參考
- https://blog.csdn.net/a19990412/article/details/82882697
由于開發環境的原因導致的不一致問題:
- 由于是在VS上開發QT文件,所以網絡上的很多QT代碼需要做修改之后才行。放心,我這里的代碼都是可以直接在VS可以運行成功的QT代碼
- 主要形式體現在:
- 導入文件的開頭變成小寫的q而不是網絡上常見的Q
- ui,在VS上不再是指針,而是一個類了。所以,操作起來會比較像Python。(這個你應該很熟)
這里的話,創建項目的方式也是非常簡單。
- 具體可以參照
- https://www.jianshu.com/p/a81350d630dd
- 文章的后半部分
我這的項目名字是: QtGuiApplicationTest
所以,在創建的文件當中,我會需要修改兩個文件。
- QtGuiApplicationTest.h
- QtGuiApplicationTest.cpp
QtGuiApplicationTest.h
#pragma once #pragma execution_character_set("utf-8") #include <QtWidgets/QMainWindow> #include "ui_QtGuiApplicationTest.h" #include <qlabel.h> #include <qpushbutton.h> #include <qgridlayout.h> #include <qlineedit.h> #include <qmessagebox.h> #include <QtNetwork\qhostinfo.h> #include <QtNetwork\qnetworkinterface.h> class QtGuiApplicationTest : public QMainWindow {Q_OBJECTpublic:QtGuiApplicationTest(QWidget *parent = Q_NULLPTR);void getHostInformation(); public slots:void slotDetail(); private:Ui::QtGuiApplicationTestClass ui;QLabel *hostLable;QLineEdit *LineEditLocalHostName;QLabel *ipLabel;QLineEdit *LineEditAddress;QPushButton * detailBtn;QGridLayout *mainLayout; };QtGuiApplicationTest.cpp
#include "QtGuiApplicationTest.h"QtGuiApplicationTest::QtGuiApplicationTest(QWidget *parent): QMainWindow(parent) {ui.setupUi(this);hostLable = new QLabel(tr("主機名:"));LineEditLocalHostName = new QLineEdit;ipLabel = new QLabel(tr("IP 地址:"));LineEditAddress = new QLineEdit;detailBtn = new QPushButton(tr("詳細"));mainLayout = new QGridLayout(ui.centralWidget);mainLayout->addWidget(hostLable, 0, 0);mainLayout->addWidget(LineEditLocalHostName, 0, 1);mainLayout->addWidget(ipLabel, 1, 0);mainLayout->addWidget(LineEditAddress, 1, 1);mainLayout->addWidget(detailBtn, 2, 0, 1, 2);getHostInformation();connect(detailBtn, SIGNAL(clicked()), this, SLOT(slotDetail())); }void QtGuiApplicationTest::getHostInformation() {QString localHostName = QHostInfo::localHostName();LineEditLocalHostName->setText(localHostName);QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();int templen = 0;QString ip = "";for (int i = 0; i < list.count(); ++i) {QNetworkInterface interface = list.at(i);QList<QNetworkAddressEntry> entryList = interface.addressEntries();if (entryList.count() < templen) continue;ip = entryList.at(entryList.count()-1).ip().toString();templen = entryList.count();}LineEditAddress->setText(ip); }void QtGuiApplicationTest::slotDetail() {QString detail = "", tempdetail="";QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();int templen = 0;for (int i = 0; i < list.count(); ++i) {QNetworkInterface interface = list.at(i);QList<QNetworkAddressEntry> entryList = interface.addressEntries();if (entryList.count() < templen) continue;tempdetail = tr("設備:") + interface.name() + "\n";tempdetail = tempdetail + tr("硬件地址:") + interface.hardwareAddress() + "\n";for (int j = 0; j < entryList.count(); ++j) {QNetworkAddressEntry entry = entryList.at(j);tempdetail = tempdetail + " " + tr("IP 地址:") + entry.ip().toString() + "\n";tempdetail = tempdetail + " " + tr("子網掩碼:") + entry.netmask().toString() + "\n";tempdetail = tempdetail + " " + tr("廣播地址:") + entry.broadcast().toString() + "\n";}detail = tempdetail;templen = entryList.count();}QMessageBox::information(this, tr("Detail"), detail); }配置上的操作
還需要把 Qt5Networkd.lib 這個庫添加到項目的鏈接器中的附加依賴項。
就把上面這個東西,復制上去就好了。(只是名字,它會自己找的。。)
運行效果
總結
以上是生活随笔為你收集整理的【VS+QT开发】获取本地网络信息小软件(C++)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【VS+QT开发】找不到到qhostin
- 下一篇: 最优二叉搜索树探究【C/C++】