Mingz技术博客

...

导航

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

 


先看一下QList类

[cpp] view plaincopy
 
  1. void MainWindow::on_pushButton_3_clicked()  
  2. {  
  3.     QList<int> list_int;  
  4.     list_int<<3<<4<<5<<6<<7<<8;  
  5.     qDebug()<<list_int.at(0);  
  6.     qDebug()<<list_int.at(1);  
  7.     qDebug()<<list_int.at(2);  
  8.     qDebug()<<list_int.at(3);  
  9.     qDebug()<<sizeof(list_int);//4  
  10.     qDebug()<<list_int;  
  11.   
  12.     QList<QString> list_str;  
  13.     list_str<<"3song"<<"4song"<<"5song"<<"6song"<<"7song"<<"8song";  
  14.     qDebug()<<list_str.at(0);  
  15.     qDebug()<<list_str.at(1);  
  16.     qDebug()<<list_str.at(2);  
  17.     qDebug()<<list_str.at(3);  
  18.     qDebug()<<sizeof(list_str);//4  
  19.     qDebug()<<list_str;  
  20.     //全部输出  
  21.   
  22.     foreach(QString str,list_str)//遍历  
  23.     {  
  24.         qDebug()<<"str is:"<<str;  
  25.     }  
  26.   
  27. }  

输出

[cpp] view plaincopy
 
  1. 3   
  2. 4   
  3. 5   
  4. 6   
  5. 4   
  6. (3, 4, 5, 6, 7, 8)   
  7.   
  8. "3song"   
  9. "4song"   
  10. "5song"   
  11. "6song"   
  12. 4   
  13. ("3song""4song""5song""6song""7song""8song")   
  14.   
  15. str is: "3song"   
  16. str is: "4song"   
  17. str is: "5song"   
  18. str is: "6song"   
  19. str is: "7song"   
  20. 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

[cpp] view plaincopy
 
  1. void MainWindow::on_pushButton_clicked()  
  2. {  
  3.     QString localHostName = QHostInfo::localHostName();//计算机名  
  4.     qDebug() <<"localHostName: "<<localHostName;  
  5.   
  6.     QHostInfo info = QHostInfo::fromName(localHostName);  
  7.     qDebug() <<"IP Address: "<<info.addresses();//ip  
  8.     foreach(QHostAddress address,info.addresses())  
  9.     {  
  10.          if(address.protocol() == QAbstractSocket::IPv4Protocol)  
  11.             qDebug() << address.toString();  
  12.     }  
  13. }  

输出

[cpp] view plaincopy
 
  1. localHostName:  "pc917"   
  2. IP Address:  (QHostAddress("192.168.1.100") ,   QHostAddress( "192.168.234.1" )  ,   QHostAddress( "192.168.183.1" )  )    
  3. "192.168.1.100"   
  4. "192.168.234.1"   
  5. "192.168.183.1"   

**************************************************************
获取本机计算机名及ip(所有),使用
QNetworkInterface::allAddresses()

[cpp] view plaincopy
 
  1. void MainWindow::on_pushButton_5_clicked()  
  2. {  
  3.     foreach (QHostAddress addr, QNetworkInterface::allAddresses()) {  
  4.         qDebug()<<addr;  
  5.         //qDebug()<<addr.toString();  
  6.     }  
  7. }  

输出

[cpp] view plaincopy
 
  1. QHostAddress( "192.168.1.100" )    
  2. QHostAddress( "192.168.234.1" )    
  3. QHostAddress( "192.168.183.1" )    
  4. QHostAddress( "127.0.0.1" )    

**************************************************************
获取指定域名或计算机名的ip,使用
QHostInfo::lookupHost

[cpp] view plaincopy
 
  1. private slots:  
  2.     void lookedUp(const QHostInfo &host);  

 

[cpp] view plaincopy
 
  1. void MainWindow::on_pushButton_4_clicked()  
  2. {  
  3.     QHostInfo::lookupHost("www.baidu.com",this,SLOT(lookedUp(QHostInfo)));  
  4.     //QHostInfo::lookupHost("192.168.1.100",this,SLOT(lookedUp(QHostInfo)));  
  5.     //QHostInfo::lookupHost("pc917",this,SLOT(lookedUp(QHostInfo)));  
  6. }  

 

[cpp] view plaincopy
 
  1. void MainWindow::lookedUp(const QHostInfo &host)  
  2. {  
  3.     qDebug() << host.addresses().first().toString();  
  4.     //qDebug() << host.addresses().first();  
  5.     qDebug() << host.hostName();  
  6. }  

输出

[cpp] view plaincopy
 
  1. QHostAddress( "119.75.217.56" )    
  2. 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

[cpp] view plaincopy
 
  1. void MainWindow::on_pushButton_2_clicked()  
  2. {  
  3.     QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();  
  4.         //获取所有网络接口的列表  
  5.         foreach(QNetworkInterface interface,list)  
  6.         {  //遍历每一个网络接口  
  7.             qDebug() << "Device: "<<interface.name();  
  8.             //设备名  
  9.             qDebug() << "HardwareAddress: "<<interface.hardwareAddress();  
  10.             //硬件地址  
  11.             QList<QNetworkAddressEntry> entryList = interface.addressEntries();  
  12.          //获取IP地址条目列表,每个条目中包含一个IP地址,一个子网掩码和一个广播地址  
  13.             foreach(QNetworkAddressEntry entry,entryList)  
  14.             {//遍历每一个IP地址条目  
  15.                 qDebug()<<"IP Address: "<<entry.ip().toString();  
  16.                 //IP地址  
  17.                 qDebug()<<"Netmask: "<<entry.netmask().toString();  
  18.                 //子网掩码  
  19.                 qDebug()<<"Broadcast: "<<entry.broadcast().toString();  
  20.                 //广播地址  
  21.             }  
  22.              qDebug() << "\n";  
  23.          }  
  24. }  

输出

[cpp] view plaincopy
 
    1. Device:  "{EE214774-B20D-428B-9194-88690C7265D8}"   
    2. HardwareAddress:  "00:A4:7E:33:52:C2"   
    3.   
    4.    
    5. Device:  "{DD72ABD5-D049-4373-BD64-CE512694AD27}"   
    6. HardwareAddress:  "00:22:FA:7B:9B:76"   
    7. IP Address:  "192.168.1.100"   
    8. Netmask:  "255.255.255.0"   
    9. Broadcast:  "192.168.1.255"   
    10.   
    11.    
    12. Device:  "{C75C0809-09F7-4DD9-9C75-34F9DC926B13}"   
    13. HardwareAddress:  "00:23:5A:6E:2E:BD"   
    14.   
    15.    
    16. Device:  "{23162CD7-FA77-403C-801C-0006C9C4CA88}"   
    17. HardwareAddress:  "00:50:56:C0:00:01"   
    18. IP Address:  "192.168.234.1"   
    19. Netmask:  "255.255.255.0"   
    20. Broadcast:  "192.168.234.255"   
    21.   
    22.    
    23. Device:  "{21D8ED0E-6FDF-4486-9FD2-2E8AC2368C1A}"   
    24. HardwareAddress:  "00:50:56:C0:00:08"   
    25. IP Address:  "192.168.183.1"   
    26. Netmask:  "255.255.255.0"   
    27. Broadcast:  "192.168.183.255"   
    28.   
    29.    
    30. Device:  "MS TCP Loopback interface"   
    31. HardwareAddress:  ""   
    32. IP Address:  "127.0.0.1"   
    33. Netmask:  ""   
    34. Broadcast:  ""   

posted on 2013-07-24 16:45  Mingz2013  阅读(299)  评论(0编辑  收藏  举报