使用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 ...
随机推荐
- hibernate 自动创建表中文乱码问题
<property name="connection.url" > <![CDATA[jdbc:mysql:///test?useUnicode=true& ...
- 基于CommonKADS方法论实现知识库系统
说明:本文是Knowledge-based systems with thecommonKADS method文章的翻译. 一.知识库系统的背景 1. 什么是知识库系统(KBS) 知识库系统是人工智能 ...
- CSS生成小三角
前言:小三角的应用场景:鼠标移动到某个按钮上面,查看信息详情时,信息详情弹出框有时候会需要一个小三角. 代码如下: <div id='triangle'></div> #tri ...
- Python基础(2) - 动态数据类型
Python是一门强类型语言,单定义变量时不需要制定类型. C#这样定义变量: ; VB这样定义变量: Python不需要制定类型,给变量赋什么类型的值,它就是什么类型.(穿神马就是神马?) > ...
- 模拟登陆Github示例
首先进入github登录页:https://github.com/login 输入账号密码,打开开发者工具,在Network页勾选上Preserve Log(显示持续日志),点击登录,查看Sessio ...
- linux设置别名
1.查看别名: alias 2.临时设置别名 alias show='ls -al' 3.永久生效 vi 家目录/.bashrc alias mmm='mysql -uroot -p' 4.删除别名 ...
- DIV固定宽度和动态拉伸混合水平排列
1.效果图 2.源代码 html <h2>1.头部固定,尾部拉伸</h2> <div class="container" id="div1& ...
- PHP开发框架CodeIgniter
CodeIgniter 是一套给 PHP 网站开发者使用的应用程序开发框架和工具包. 通过下面四步来安装 CodeIgniter: 解压缩安装包: 将 CodeIgniter 文件夹及里面的文件上传到 ...
- Heka 的 CMake 编译配置分析
CMake 是一个跨平台的自动化建构系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程,可以产生标准的构建文件. CMakeLists.txt 的语法比较简单,由命令.注释和 ...
- [Java反射基础三]方法反射的基本操作
本文接上文“获取类的信息”,利用反射(invoke)来获取一个类中的方法来执行. 1.定义一个类,包含三个名称相同,参数不同的方法 class A{ public void print(){ Syst ...