QT擷取本機網路資訊

來源:互聯網
上載者:User

直接上代碼:

 QString detail="";    QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();    for(int i=0;i<list.count();i++)    {        QNetworkInterface interface=list.at(i);        detail=detail+tr("裝置:")+interface.name()+"\n";        detail+=tr("硬體地址:")+interface.hardwareAddress()+"\n";        QList<QNetworkAddressEntry> entryList=interface.addressEntries();        for(int j=0;j<entryList.count();j++)        {            QNetworkAddressEntry entry=entryList.at(j);            detail+="\t"+tr("ip地址:")+entry.ip().toString()+"\n";            detail+="\t"+tr("子網路遮罩:")+entry.netmask().toString()+"\n";            detail+="\t"+tr("廣播位址:")+entry.broadcast().toString()+"\n";        }    }    QMessageBox::information(this,tr("Detail"),detail);<span style="color:#c0c0c0;"></span>

接下來解釋每行代碼的意思:

定義一個字串detail,擷取所有網路介面存到list中,這裡指所有在同一個網路下的主機。迴圈遍曆所有主機開始取每台主機的資訊,裝置和硬體地址可以直接取,IP地址。子網路遮罩,廣播位址又是一個實體類。最後將取出來的字串放到QMessageBox控制項上顯示,用法如上,貌似QMessageBox還有很多變異用法,好可怕,慢慢來。

    

 QString localHostName=QHostInfo::localHostName();    qDebug()<<localHostName;    ui->lineEdit->setText(localHostName);    QHostInfo hostinfo=QHostInfo::fromName(localHostName);    QList<QHostAddress> listAdress=hostinfo.addresses();    if(!listAdress.isEmpty())    {            qDebug()<<"list不為空白";      ui->lineEdit_2->setText(listAdress.first().toString());
讀取本機名和IP地址。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.