标签:exec 分享 分享图片 this 接口 http exe 硬件 rom
在pro文件中加入如下代码
QT += network
widget.h中的代码如下
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QMessageBox>
#include <QHostInfo>
#include <QNetworkInterface>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void getHostInformation();
public slots:
void slotDetail();
private:
QLabel *hostLabel;
QLineEdit *LineEditLocalHostName;
QLabel *ipLabel;
QLineEdit *LineEditAddress;
QPushButton *detailBtn;
QGridLayout *mainLayout;
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp中的代码如下
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QNetworkAddressEntry>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
hostLabel=new QLabel(tr("主机名:"));
LineEditLocalHostName=new QLineEdit();
ipLabel=new QLabel(tr("IP地址:"));
LineEditAddress=new QLineEdit();
detailBtn=new QPushButton(tr("详细"));
mainLayout=new QGridLayout(this);
mainLayout->addWidget(hostLabel,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(bool)),
this,SLOT(slotDetail()));
}
//获取详细信息函数
void Widget::getHostInformation()
{
QString localHostName=QHostInfo::localHostName(); //获取本机主机名
LineEditLocalHostName->setText(localHostName);
//根据主机名获取相关的IP地址
QHostInfo hostInfo=QHostInfo::fromName(localHostName);
//获取主机的IP地址列表
QList<QHostAddress> listAddress=hostInfo.addresses();
if(!listAddress.isEmpty())
{
LineEditAddress->setText(listAddress.at(2).toString());
}
}
//“详细”按键按下时的槽函数
void Widget::slotDetail()
{
QString detail="";
//QNetWorkInterface类提供了一个主机IP地址和网络接口的列表
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
for(int i=0;i<list.count();i++)
{
QNetworkInterface interface=list.at(i);
//获得网络接口的名称
detail=detail+tr("设备")+interface.name()+"\n";
//获得网络接口的硬件地址
detail=detail+tr("硬件")+interface.hardwareAddress()+"\n";
QList<QNetworkAddressEntry> entryList=interface.addressEntries();
for(int j=1;j<entryList.count();j++)
{
QNetworkAddressEntry entry=entryList.at(j);
detail=detail+"\t"+tr("IP地址:")+entry.ip().toString()+"\n";
detail=detail+"\t"+tr("子网掩码:")+entry.netmask().toString()+"\n";
detail=detail+"\t"+tr("广播地址:")+entry.broadcast().toString()+"\n";
}
} //end for(int i=0;i<list.count();i++)
QMessageBox::information(this,tr("Detail"),detail);
}
Widget::~Widget()
{
delete ui;
}
main.cpp中的代码如下
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
运行效果如下
标签:exec 分享 分享图片 this 接口 http exe 硬件 rom
原文地址:https://www.cnblogs.com/Manual-Linux/p/9542582.html