效果展现
代码实现
networkinformation.h
:
#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
class NetworkInformation : public QMainWindow
{
Q_OBJECT
public:
NetworkInformation(QWidget *parent = nullptr);
~NetworkInformation();
void getHostInfo();
public slots:
void slotDetail();
private:
QLabel * hostNameLabel;
QLineEdit * hostNameLineEdit;
QLabel * ipLabel;
QLineEdit * ipLineEdit;
QPushButton * detailBtn;
};
#endif // NETWORKINFORMATION_H
networkinformation.cpp
:文章来源:https://www.toymoban.com/news/detail-406605.html
#include "networkinformation.h"
#include <QGridLayout>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QMessageBox>
#include <QDebug>
NetworkInformation::NetworkInformation(QWidget *parent)
: QMainWindow(parent)
{
QWidget * widget = new QWidget();
this->setCentralWidget(widget);
hostNameLabel = new QLabel(tr("主机名:"));
hostNameLineEdit = new QLineEdit;
ipLabel = new QLabel(tr("IP地址:"));
ipLineEdit = new QLineEdit;
detailBtn = new QPushButton(tr("详细"));
QGridLayout * mainLayout = new QGridLayout;
mainLayout->addWidget(hostNameLabel,0,0);
mainLayout->addWidget(hostNameLineEdit,0,1);
mainLayout->addWidget(ipLabel,1,0);
mainLayout->addWidget(ipLineEdit,1,1);
mainLayout->addWidget(detailBtn,2,0,1,2);
widget->setLayout(mainLayout);
//获取主机信息
getHostInfo();
//点击详细按钮 显示详细的 网络信息
connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));
}
void NetworkInformation::getHostInfo(){
//主机名
QString hostName = QHostInfo::localHostName();
hostNameLineEdit->setText(hostName);
//ip地址
QHostInfo hostInfo = QHostInfo::fromName(hostName);
QList<QHostAddress> list = hostInfo.addresses();
qDebug()<<"size = "<<list.count()<<endl;
for(int i = 0;i < list.count();i++){
qDebug()<<list.at(i).toString()<<endl;
}
if(list.count() >= 3){
ipLineEdit->setText(list.at(3).toString());
}
}
void NetworkInformation::slotDetail(){
QString detail = "";
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
for(int i = 0;i < list.count();i++){
QNetworkInterface interface = list.at(i);
detail += tr("设备:") + interface.name() + "\n";
detail += tr("硬件地址:") + interface.hardwareAddress() + "\n";
QList<QNetworkAddressEntry> entryList = interface.addressEntries();
for(int j = 0;j < entryList.count();j++){
QNetworkAddressEntry entry = entryList.at(j);
detail += "\t" + tr("IP地址:") + entry.ip().toString() + "\n";
detail += "\t" + tr("子网掩码:") + entry.netmask().toString() + "\n";
detail += "\t" + tr("广播地址:") + entry.broadcast().toString() + "\n";
}
}
qDebug()<<detail<<endl;
QMessageBox::information(this,tr("Detail"),detail);
}
NetworkInformation::~NetworkInformation()
{
}
main.cpp
:文章来源地址https://www.toymoban.com/news/detail-406605.html
#include "networkinformation.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NetworkInformation w;
w.show();
return a.exec();
}
到了这里,关于Qt——实现一个获取本机网络信息的界面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!