GetNetWorkInfo::GetNetWorkInfo(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
//获得主机名
QString localHostName = QHostInfo::localHostName();
ui.hostNameLineEdit->setText(localHostName);
//通过主机名获得主机信息
QHostInfo hostInfo = QHostInfo::fromName(localHostName);
QList<QHostAddress>listAddress = hostInfo.addresses();
if (!listAddress.isEmpty())
{
ui.addressLineEdit->setText(listAddress.first().toString());
}
QString detail = "";
//获取所有所有网络接口列表
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
for (int i = 0;i<list.count();i++)
{
QNetworkInterface interface = list.at(i);
//获得硬件名称
detail = detail + tr("Device") + interface.name() + "\n";
QString hardwareAddress = interface.hardwareAddress();
//获得硬件地址
detail = detail + tr("HardWareAddress:") + interface.hardwareAddress() + "\n";
//返回各个接口的
QList<QNetworkAddressEntry> entryList = interface.addressEntries();
for (int j = 0;j<entryList.count(); j++)
{
QNetworkAddressEntry entry = entryList.at(j);
detail = detail + "\t" + tr("IP Address:") +entry.ip().toString() + "\n";
detail = detail + "\t" + tr("Netmask:") +entry.netmask().toString() + "\n";
detail = detail + "\t" + tr("Broadcast:") +entry.broadcast().toString() + "\n";
}
}
ui.detailTextEdit->setPlainText(detail);
}