使用qt的hostInfo类,查看本机的IP和设备
创建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和设备的更多相关文章
- 查看本机的IP地址方法:
查看本机的IP地址方法:对于XP系统:方法一:如果右下角系统托盘区有本地连接的小电脑,双击小电脑→支持,就可以看到本机IP地址.无线连接也是一样.方法二:开始→运行cmd /k ipconfig,IP ...
- (转)linux获取/查看本机出口ip
获取/查看本机出口ip curl http://members.3322.org/dyndns/getip 1 curl ifconfig.me 2 #或者 3 curl http://member ...
- 查看本机的ip地址
ifconfig可以查看本机的ip地址:inet addr:10.108.104.185
- 查看本机的ip
1.Windows 1.1.查看ip地址 ipconfig 1.2.查看MAC地址 ipconfig /all 2.Linux 2.1.查看ip地址 ifconfig 2.2.查看MAC地址 ip l ...
- Ubuntu 查看本机的ip
打开终端中执行:ifconfig -a命令即可,如下图所示白色背景信息即是. 说明: enp0s3 表示第一块网卡, 其中 HWaddr 表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址 ...
- CentOS查看本机公网IP命令
icanhazip.com 使你在任何地方知道你的公网IP地址 icanhazip.com是一个网址,你在浏览器中输入这个网址,你就能得到你的公网IP地址了. 我在Linux下一般使用curl ica ...
- 转载:CentOS查看本机公网IP命令
icanhazip.com 使你在任何地方知道你的公网IP地址 icanhazip.com是一个网址,你在浏览器中输入这个网址,你就能得到你的公网IP地址了. 我在Linux下一般使用curl ica ...
- Mac 命令行查看自己的公网IP
Mac 查看本机公网IP 命令 curl ifconfig.me 如果想更好看一点 curl ipinfo.io/json 还可以用wget wget http://ipecho.net/plain ...
- Window、Linux查看本机外网ip
前言 我们上网用的IP并不一定是本机网卡的IP地址,由于公网IP地址稀少,国内绝大多数电脑上网,一般都是通过拨号或者端口映射.多个内网地址映射到一个公网地址来实现上网的. 目录 1.查看本机网卡ip ...
随机推荐
- C#常用的引用
1.使用ConfigurationManager需要在.net引用中添加System.Configuration引用 2.使用HttpContext需要在.net引用中添加System.Web引用
- MySQL查询操作select
查找记录 SELECT select_expr [,select_expr ...] [ FROM table_references(表的参照) [WHERE where_condition](条件) ...
- php添加mysql.so扩展
1.软件包安装 yum install php-mysql 安装的是mariadb的扩展 yum install php-mysqlnd 安装的是mysql的扩展
- orcale 之 集合操作
集合操作就是将两个或者多个 sql 查询的结果合并成复合查询.常见的集合操作有UNION(并运算).UNION ALL.INTERSECT(交运算)和MINUS(差运算). UNION UNION 运 ...
- GITHUB一个新的项目发布
经过一段时间的积累,写了一些代码,发现好多功能有好几个系统都在用,但是公司的开发过程中,并没有一个对通用功能提取整合普遍化的一个流程,所以就自己将在项目开发过程中遇到的一些功能提取出来,并尽量做到普适 ...
- 常用工具说明--GitHub团队项目合作流程
注:其中 零.一.七 是由团队项目负责人来完成的.开发人员只要从 二 开始就行了. 零.前期准备: 首先把队友直接push的权限关掉,即设置成Read.这样可以防止队友误操作,未经审核就把代码push ...
- url字符转义
作者在做短链接功能时,url参数里带了&字符,结果无法转换.后来查了一下,发现可以用其它符号代替.下面是对应表 + URL 中+号表示空格 ...
- SQL语句整理(二) 数据定义语言DDL
前言: 这是我学数据库时整理的学习资料,基本上包括了所以的SQL语句的知识点. 我的教材是人大王珊老师的<数据库系统概论>. 因为是手打的,所以会用一些细节打错了,但都挺明显也不多(考完试 ...
- html--深入理解4种dom对象
这四个对象是从HTML结构中逐层深入的,分别代表了HTML结构中所有的内容: 1.Document对象 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们 ...
- flask接收前台的ajax的post数据
html <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...