创建NetWorkInformation类,main.cpp直接生成。

 #include "networkinformation.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NetworkInformation w;
w.show(); return a.exec();
}

头文件声明要写的槽函数,还有布局控件。

 #ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H #include <QWidget>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
class NetworkInformation : public QWidget
{
Q_OBJECT public:
NetworkInformation(QWidget *parent = );
~NetworkInformation();
public:
void getHostInformation();
public slots:
void slotDetail();
private:
QLabel *hostLabel;
QLineEdit *LineEditLocalHostname;
QLabel * ipLabel;
QLineEdit * LineEditAddress;
QPushButton* detailBtn;
QGridLayout * mainLayout;
}; #endif // NETWORKINFORMATION_H

最后是.cpp的文件。

在构造函数里写控件的布局,然后实现槽函数。

 #include "networkinformation.h"
#include <QMessageBox>
NetworkInformation::NetworkInformation(QWidget *parent)
: QWidget(parent)
{
hostLabel = new QLabel("name:");
LineEditLocalHostname = new QLineEdit;
ipLabel = new QLabel(tr("ip:"));
LineEditAddress = new QLineEdit;
detailBtn = new QPushButton(tr("detail"));
mainLayout =new QGridLayout(this); mainLayout->addWidget(hostLabel,,);
mainLayout->addWidget(LineEditLocalHostname,,);
mainLayout->addWidget(ipLabel,,);
mainLayout->addWidget(LineEditAddress,,);
mainLayout->addWidget(detailBtn,,,,);
getHostInformation();
connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));
} NetworkInformation::~NetworkInformation()
{ }
void NetworkInformation::getHostInformation()
{
QString localHostName = QHostInfo::localHostName();
LineEditLocalHostname->setText(localHostName); QHostInfo hostinfo = QHostInfo::fromName(localHostName); foreach (const QHostAddress &address, hostinfo.addresses())
{ if (address.protocol() == QAbstractSocket::IPv4Protocol)
{
LineEditAddress->setText((address.toString()));
}
}
}
void NetworkInformation::slotDetail()
{
QString detail="";
QList<QNetworkInterface>list=QNetworkInterface::allInterfaces();
for(int i=;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 =;j<entryList.count();j++)
{
/QNetworkAddressEntry entry = entryList.at(i);
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";
}
}
QMessageBox::information(this,tr("Detail"),detail);
}

使用qt的hostInfo类,查看本机的IP和设备的更多相关文章

  1. 查看本机的IP地址方法:

    查看本机的IP地址方法:对于XP系统:方法一:如果右下角系统托盘区有本地连接的小电脑,双击小电脑→支持,就可以看到本机IP地址.无线连接也是一样.方法二:开始→运行cmd /k ipconfig,IP ...

  2. (转)linux获取/查看本机出口ip

    获取/查看本机出口ip  curl http://members.3322.org/dyndns/getip 1 curl ifconfig.me 2 #或者 3 curl http://member ...

  3. 查看本机的ip地址

    ifconfig可以查看本机的ip地址:inet addr:10.108.104.185

  4. 查看本机的ip

    1.Windows 1.1.查看ip地址 ipconfig 1.2.查看MAC地址 ipconfig /all 2.Linux 2.1.查看ip地址 ifconfig 2.2.查看MAC地址 ip l ...

  5. Ubuntu 查看本机的ip

    打开终端中执行:ifconfig -a命令即可,如下图所示白色背景信息即是. 说明: enp0s3 表示第一块网卡, 其中 HWaddr 表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址 ...

  6. CentOS查看本机公网IP命令

    icanhazip.com 使你在任何地方知道你的公网IP地址 icanhazip.com是一个网址,你在浏览器中输入这个网址,你就能得到你的公网IP地址了. 我在Linux下一般使用curl ica ...

  7. 转载:CentOS查看本机公网IP命令

    icanhazip.com 使你在任何地方知道你的公网IP地址 icanhazip.com是一个网址,你在浏览器中输入这个网址,你就能得到你的公网IP地址了. 我在Linux下一般使用curl ica ...

  8. Mac 命令行查看自己的公网IP

    Mac 查看本机公网IP 命令 curl ifconfig.me 如果想更好看一点 curl ipinfo.io/json 还可以用wget wget http://ipecho.net/plain ...

  9. Window、Linux查看本机外网ip

    前言 我们上网用的IP并不一定是本机网卡的IP地址,由于公网IP地址稀少,国内绝大多数电脑上网,一般都是通过拨号或者端口映射.多个内网地址映射到一个公网地址来实现上网的. 目录 1.查看本机网卡ip ...

随机推荐

  1. mysql Access denied for user root@localhost错误解决方法总结

    原文:http://www.111cn.net/database/mysql/44142.htm Access denied for user 'root'@'localhost' (using pa ...

  2. DataAdapter对象

    DataAdapter对象在物理数据库表和内存数据表(结果集)之间起着桥梁的作用.它通常与DataTable对象或DataSet对象配合来实现对数据库的操作. DataAdapter对象是一个双向通道 ...

  3. CSS禁止滚动条

    CSS禁止滚动条的方法: 1.完全隐藏 在<boby>里加入scroll="no",可隐藏滚动条: <boby scroll="no"> ...

  4. Redis的安装(CentOS 7下)

    redis的官网,www.redis.io 1. 先下载 redis: wget http://download.redis.io/releases/redis-3.0.3.tar.gz 2. 解压: ...

  5. SSIS教程:创建简单的ETL包

    SSIS: Microsoft SQL Server Integration Services.是一个可用于生成高性能数据集成解决方案的平台,其中包括数据仓库的提取(Extract).转换(Trans ...

  6. sublime text 2编辑器中文问题

    Sublime Text 2是一个非常不错的源代码及文本编辑器,但是不支持GB2312和GBK编码在很多情况下会非常麻烦.不过Sublime Package Control所以供的插件可以让Subli ...

  7. js 判断id 是否存在

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. hdu1385 最短路字典序

    http://blog.csdn.net/ice_crazy/article/details/7785111 http://blog.csdn.net/shuangde800/article/deta ...

  9. php获取今日开始时间和结束时间

    $begintime=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y'))); $endtime=date( ...

  10. FLASK日志记录

    from flask import Flask from flask_restful import Resource, Api import logging app = Flask(__name__) ...