qt4 network----QList

本文介绍如何使用Qt库获取本机网络信息,包括计算机名称、IP地址、网络接口详情等,并演示了多种Qt类如QHostInfo、QHostAddress、QNetworkInterface的应用。

四十五、Qt网络(五)获取本机网络信息


先看一下QList类
void MainWindow::on_pushButton_3_clicked()
{
    QList<int> list_int;
    list_int<<3<<4<<5<<6<<7<<8;
    qDebug()<<list_int.at(0);
    qDebug()<<list_int.at(1);
    qDebug()<<list_int.at(2);
    qDebug()<<list_int.at(3);
    qDebug()<<sizeof(list_int);//4
    qDebug()<<list_int;

    QList<QString> list_str;
    list_str<<"3song"<<"4song"<<"5song"<<"6song"<<"7song"<<"8song";
    qDebug()<<list_str.at(0);
    qDebug()<<list_str.at(1);
    qDebug()<<list_str.at(2);
    qDebug()<<list_str.at(3);
    qDebug()<<sizeof(list_str);//4
    qDebug()<<list_str;
    //全部输出

    foreach(QString str,list_str)//遍历
    {
        qDebug()<<"str is:"<<str;
    }

}
输出
3 
4 
5 
6 
4 
(3, 4, 5, 6, 7, 8) 

"3song" 
"4song" 
"5song" 
"6song" 
4 
("3song", "4song", "5song", "6song", "7song", "8song") 

str is: "3song" 
str is: "4song" 
str is: "5song" 
str is: "6song" 
str is: "7song" 
str is: "8song" 

QList是一个类模板,保存某类型实例的指针,用QList操作批量同类型数据很快
使用foreach遍历QList比较方便

Internally, QList<T> is represented as an array of pointers to items of type T. If T is itself a pointer type or a basic type that is no larger than a pointer, or if T is one of Qt's shared classes, then QList<T> stores the items directly in the pointer array. For lists under a thousand items, this array representation allows for very fast insertions in the middle, and it allows index-based access. Furthermore, operations like prepend() and append() are very fast, because QList preallocates memory at both ends of its internal array. (See Algorithmic Complexity for details.) Note, however, that for unshared list items that are larger than a pointer, each append or insert of a new item requires allocating the new item on the heap, and this per item allocation might make QVector a better choice in cases that do lots of appending or inserting, since QVector allocates memory for its items in a single heap allocation.

Note that the internal array only ever gets bigger over the life of the list. It never shrinks. The internal array is deallocated by the destructor and by the assignment operator, when one list is assigned to another.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
使用到的类有
QHostInfo
QHostAddress
QNetworkInterface
QNetworkAddressEntry
**************************************************************

获取本机计算机名及ip(所有),使用
QString QHostInfo::localHostName () [static]
QList<QHostAddress> QHostInfo::addresses () const

void MainWindow::on_pushButton_clicked()
{
    QString localHostName = QHostInfo::localHostName();//计算机名
    qDebug() <<"localHostName: "<<localHostName;

    QHostInfo info = QHostInfo::fromName(localHostName);
    qDebug() <<"IP Address: "<<info.addresses();//ip
    foreach(QHostAddress address,info.addresses())
    {
         if(address.protocol() == QAbstractSocket::IPv4Protocol)
            qDebug() << address.toString();
    }
}
输出
localHostName:  "pc917" 
IP Address:  (QHostAddress("192.168.1.100") ,   QHostAddress( "192.168.234.1" )  ,   QHostAddress( "192.168.183.1" )  )  
"192.168.1.100" 
"192.168.234.1" 
"192.168.183.1" 
**************************************************************
获取本机计算机名及ip(所有),使用
QNetworkInterface ::allAddresses ()
void MainWindow::on_pushButton_5_clicked()
{
    foreach (QHostAddress addr, QNetworkInterface::allAddresses()) {
        qDebug()<<addr;
        //qDebug()<<addr.toString();
    }
}
输出
 QHostAddress( "192.168.1.100" )  
 QHostAddress( "192.168.234.1" )  
 QHostAddress( "192.168.183.1" )  
 QHostAddress( "127.0.0.1" )  
**************************************************************
获取指定域名或计算机名的ip,使用
QHostInfo::lookupHost
private slots:
    void lookedUp(const QHostInfo &host);

void MainWindow::on_pushButton_4_clicked()
{
    QHostInfo::lookupHost("www.baidu.com",this,SLOT(lookedUp(QHostInfo)));
    //QHostInfo::lookupHost("192.168.1.100",this,SLOT(lookedUp(QHostInfo)));
    //QHostInfo::lookupHost("pc917",this,SLOT(lookedUp(QHostInfo)));
}

void MainWindow::lookedUp(const QHostInfo &host)
{
    qDebug() << host.addresses().first().toString();
    //qDebug() << host.addresses().first();
    qDebug() << host.hostName();
}
输出
 QHostAddress( "119.75.217.56" )  
"www.baidu.com" 
**************************************************************
获取所有网络接口的信息--所有网卡,使用
QNetworkInterface::allInterfaces();
QString QNetworkInterface::name () const
QString QNetworkInterface::hardwareAddress () const

QHostAddress QNetworkAddressEntry::ip () const
QHostAddress QNetworkAddressEntry::netmask () const
QHostAddress QNetworkAddressEntry::ip () const
void MainWindow::on_pushButton_2_clicked()
{
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
        //获取所有网络接口的列表
        foreach(QNetworkInterface interface,list)
        {  //遍历每一个网络接口
            qDebug() << "Device: "<<interface.name();
            //设备名
            qDebug() << "HardwareAddress: "<<interface.hardwareAddress();
            //硬件地址
            QList<QNetworkAddressEntry> entryList = interface.addressEntries();
         //获取IP地址条目列表,每个条目中包含一个IP地址,一个子网掩码和一个广播地址
            foreach(QNetworkAddressEntry entry,entryList)
            {//遍历每一个IP地址条目
                qDebug()<<"IP Address: "<<entry.ip().toString();
                //IP地址
                qDebug()<<"Netmask: "<<entry.netmask().toString();
                //子网掩码
                qDebug()<<"Broadcast: "<<entry.broadcast().toString();
                //广播地址
            }
             qDebug() << "\n";
         }
}
输出
Device:  "{EE214774-B20D-428B-9194-88690C7265D8}" 
HardwareAddress:  "00:A4:7E:33:52:C2" 

 
Device:  "{DD72ABD5-D049-4373-BD64-CE512694AD27}" 
HardwareAddress:  "00:22:FA:7B:9B:76" 
IP Address:  "192.168.1.100" 
Netmask:  "255.255.255.0" 
Broadcast:  "192.168.1.255" 

 
Device:  "{C75C0809-09F7-4DD9-9C75-34F9DC926B13}" 
HardwareAddress:  "00:23:5A:6E:2E:BD" 

 
Device:  "{23162CD7-FA77-403C-801C-0006C9C4CA88}" 
HardwareAddress:  "00:50:56:C0:00:01" 
IP Address:  "192.168.234.1" 
Netmask:  "255.255.255.0" 
Broadcast:  "192.168.234.255" 

 
Device:  "{21D8ED0E-6FDF-4486-9FD2-2E8AC2368C1A}" 
HardwareAddress:  "00:50:56:C0:00:08" 
IP Address:  "192.168.183.1" 
Netmask:  "255.255.255.0" 
Broadcast:  "192.168.183.255" 

 
Device:  "MS TCP Loopback interface" 
HardwareAddress:  "" 
IP Address:  "127.0.0.1" 
Netmask:  "" 
Broadcast:  "" 


### Qt 蓝牙模块不可用解决方案 当遇到 **Qt Bluetooth module not working** 的问题时,可能涉及多个方面的原因,包括环境配置错误、依赖库缺失以及硬件兼容性等问题。以下是针对该问题的分析和解决方法: #### 1. 检查开发环境配置 确保已安装并启用了支持蓝牙功能的相关组件。对于基于 Windows 或 Linux 平台的应用程序开发,需确认以下事项: - 安装了完整的 Qt SDK 版本,并包含了 `qtconnectivity` 组件的支持[^1]。 - 验证是否正确设置了编译器路径及其关联工具链。 如果使用的是 MinGW 编译器,在某些情况下可能会因为缺少特定动态链接库而导致蓝牙功能失效;此时可以尝试切换到 MSVC 工具集来规避此类潜在冲突现象。 #### 2. 更新系统与驱动程序 保持操作系统及相关设备驱动处于最新状态非常重要。旧版本可能存在 bug 导致无法正常工作或者性能下降等情况发生。例如在 Ubuntu 上可以通过运行如下命令更新软件包列表及升级现有安装项至最新稳定版次级发行号(release candidate)[^2]: ```bash sudo apt update && sudo apt full-upgrade -y ``` 同时也要注意检查电脑内置或外接 USB 类型适配器固件是否存在可用更新选项可供下载应用于此处提到的产品线当中(如Broadcom BCM系列芯片组)。 #### 3. 修改项目 .pro 文件设置 为了使应用程序能够访问系统的蓝牙资源,请确保您的 `.pro` 文件中包含必要的模块声明语句: ```plaintext QT += bluetooth core network widgets gui CONFIG += c++17 ``` 上述代码片段中的每一部分都扮演着重要角色——它们共同协作以实现预期的功能需求。 另外还需特别留意目标平台架构差异所带来的影响因素(比如 ARM vs x86),这可能会影响到最终构建产物能否成功加载所需服务接口定义文件(.so/.dll). #### 4. 测试最小化示例代码验证基本操作流程可行性 创建一个新的控制台应用程序工程结构作为起点位置来进行初步排查定位具体哪里出了差错比较好一些; 下面给出了一段简单的测试脚本来帮助快速上手体验一下基础 API 使用方式 : ```cpp #include <QCoreApplication> #include <QBluetoothLocalDevice> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); qDebug() << "Supported Bluetooth versions:" << QBluetoothLocalDevice::supportedFeatures(); QList<QBluetoothHostInfo> hosts = QBluetoothLocalDevice::allDevices(); foreach (const QBluetoothHostInfo &host, hosts){ qDebug()<<"Name:"<< host.name() <<"Address:"<<host.address().toString(); } return app.exec(); } ``` 此段源码主要完成两项任务:一是打印当前环境中所支持的蓝牙协议栈特性集合情况报告;二是枚举列出所有可检测得到本地主机信息记录条目详情数据展示出来供后续进一步处理调用参考依据之用. --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值