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"
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
**************************************************************
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"**************************************************************
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" )**************************************************************
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"**************************************************************
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: ""