【VS+QT開發】獲取本地網路資訊小軟體(C++)
阿新 • • 發佈:2018-12-12
簡介
考慮到之前的那一個安裝可能實現上有點複雜,也不知道你願不願意看。 所以,這裡就實現一個簡單的。 對了,考慮到我垃圾般的程式設計師審美,所以,如果覺得不好看的話,後期可以自己嘗試看看能不能挑一下顏色,圖片等一系列操作的。
- 但是,下面的這版本,就是全用程式碼實現的。
- 原因很簡單,就是我擔心你不太會用Qt designer
- 但是,其實這個還是蠻簡單的。就是簡單大拖動一下就好。只需要記住這個ui的這個控制元件,叫什麼名字,自己後期針對性的控制一下就好了。
開發環境:
由於開發環境的原因導致的不一致問題:
- 由於是在VS上開發QT檔案,所以網路上的很多QT程式碼需要做修改之後才行。放心,我這裡的程式碼都是可以直接在VS可以執行成功的QT程式碼
- 主要形式體現在:
- 匯入檔案的開頭變成小寫的q而不是網路上常見的Q
- ui,在VS上不再是指標,而是一個類了。所以,操作起來會比較像Python。(這個你應該很熟)
這裡的話,建立專案的方式也是非常簡單。
我這的專案名字是: 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_OBJECT
public:
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
這個庫新增到專案的連結器中的附加依賴項。
就把上面這個東西,複製上去就好了。(只是名字,它會自己找的。。)