天天看點

Qt檢視主機資訊如ip/hardware address/netmask等

注意要在.pro中添加

QT += network

1、通過QHostInfo擷取主機名,在通過主機名可以擷取ip

2、通過QNetworkInterface可以擷取本機的所有網絡接口,即可以擷取接口中的name,hardware address

3、又可以通過網絡接口擷取網絡位址如IP,NETMASK等,即QNetworkInterface的成員函數addressEntries()位址入口函數;

其還回值為QList<QNetworkAddressEntry> 那麼就可以擷取各類位址了!

如截圖:

Qt檢視主機資訊如ip/hardware address/netmask等
Qt檢視主機資訊如ip/hardware address/netmask等
#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H

#include <QDialog>
class QLabel;
class QLineEdit;

class NetworkInformation: public QDialog
{
    Q_OBJECT

private:
    QLabel *nameLabel;
    QLabel *ipLabel;
    QLineEdit *nameEdit;
    QLineEdit *ipEdit;
    QPushButton *detailButton;

public:
    NetworkInformation(QWidget *parent = 0);

private slots:
    void slotDetail();
};

#endif // NETWORKINFORMATION_H
           
#include "networkinformation.h"

#include <QtGui>
#include <QHostInfo>
#include <QNetworkInterface>

NetworkInformation::NetworkInformation(QWidget *parent)
    : QDialog(parent)
{
    nameLabel = new QLabel(tr("Name:"));
    nameEdit = new QLineEdit;
    nameLabel->setBuddy(nameEdit);
    nameEdit->setReadOnly(true);
    QString name = QHostInfo::localHostName();
    nameEdit->setText(name);

    ipLabel = new QLabel(tr("Ip:"));
    ipEdit = new QLineEdit;
    ipLabel->setBuddy(ipEdit);
    ipEdit->setReadOnly(true);
    QHostInfo hostInfo = QHostInfo::fromName(name);
    QList<QHostAddress> listAddress = hostInfo.addresses();
    if(!listAddress.empty())
        ipEdit->setText(listAddress.first().toString());

    detailButton = new QPushButton(tr("Detail"));
    connect(detailButton, SIGNAL(clicked()),
            this, SLOT(slotDetail()));

    QHBoxLayout *layout1 = new QHBoxLayout;
    layout1->addWidget(nameLabel);
    layout1->addWidget(nameEdit);

    QHBoxLayout *layout2 = new QHBoxLayout;
    layout2->addWidget(ipLabel);
    layout2->addWidget(ipEdit);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(layout1);
    mainLayout->addLayout(layout2);
    mainLayout->addWidget(detailButton);
    setLayout(mainLayout);

    setWindowTitle(tr("Local Host Information"));
}

//private slots
void NetworkInformation::slotDetail()
{
    QString detail = "";
    QList<QNetworkInterface> listNetwork = QNetworkInterface::allInterfaces();
    for(int i = 0; i < listNetwork.count(); i++) {
        detail += "name: " + listNetwork.at(i).name() + "\n";
        detail += "hardware address: " + listNetwork.at(i).hardwareAddress() + "\n";
        QList<QNetworkAddressEntry> listAddress = listNetwork.at(i).addressEntries();
        for(int j = 0; j < listAddress.count(); j++) {
            detail += "ip: " + listAddress.at(j).ip().toString() + "\n";
            detail += "netmask: " + listAddress.at(j).netmask().toString() + "\n";
            detail += "broadcase: " + listAddress.at(j).broadcast().toString() + "\n";
        }
    }
    QMessageBox::warning(this, tr("Detail"), detail);
}








           
#include "networkinformation.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    NetworkInformation *network = new NetworkInformation;
    network->show();

    return app.exec();
}
           

繼續閱讀