简介
考虑到之前的那一个安装可能实现上有点复杂,也不知道你愿不愿意看。
所以,这里就实现一个简单的。
对了,考虑到我垃圾般的程序员审美,所以,如果觉得不好看的话,后期可以自己尝试看看能不能挑一下颜色,图片等一系列操作的。
但是,下面的这版本,就是全用代码实现的。原因很简单,就是我担心你不太会用Qt designer但是,其实这个还是蛮简单的。就是简单大拖动一下就好。只需要记住这个ui的这个控件,叫什么名字,自己后期针对性的控制一下就好了。
开发环境:
VS + QT
关于上面的这个东西怎么配置可以参考/a19990412/article/details/82882697
由于开发环境的原因导致的不一致问题:
由于是在VS上开发QT文件,所以网络上的很多QT代码需要做修改之后才行。放心,我这里的代码都是可以直接在VS可以运行成功的QT代码主要形式体现在:导入文件的开头变成小写的q而不是网络上常见的Qui,在VS上不再是指针,而是一个类了。所以,操作起来会比较像Python。(这个你应该很熟)
这里的话,创建项目的方式也是非常简单。
具体可以参照/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("子网掩码:") + mask().toString() + "\n";tempdetail = tempdetail + " " + tr("广播地址:") + entry.broadcast().toString() + "\n";}detail = tempdetail;templen = entryList.count();}QMessageBox::information(this, tr("Detail"), detail);}
配置上的操作
还需要把Qt5Networkd.lib
这个库添加到项目的链接器中的附加依赖项。
就把上面这个东西,复制上去就好了。(只是名字,它会自己找的。。)