posts - 1,  comments - 0,  trackbacks - 0

基于Qt写的一个关于如何获取主机信息的源码
#include "hoinfo.h"Friday, June 29, 2012


#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTextCodec>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QString>
#include <QList>
#include <QtNetwork>
#include <QHostInfo>
#include <QHostAddress>
#include <QMessageBox>


HoInfo::HoInfo(QWidget *parent)
    : QWidget(parent)
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
    hostLabel = new QLabel(tr("主机名"));
    IpLabel = new QLabel(tr("Ip地址"));
    hostLE = new QLineEdit;
    //hostLE->setText(localHostName);
    IpLE = new QLineEdit;
    detailPB = new QPushButton(tr("详细"));

    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(hostLabel,0,0);
    gridLayout->addWidget(hostLE,0,1);
    gridLayout->addWidget(IpLabel,1,0);
    gridLayout->addWidget(IpLE,1,1);

    QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    vboxLayout->addLayout(gridLayout);
    vboxLayout->addWidget(detailPB);

    connect(detailPB,SIGNAL(clicked()),this,SLOT(slotDetail()));
    connect(hostLE,SIGNAL(cursorPositionChanged(int,int)),SLOT(getHostInformation()));
    connect(IpLE,SIGNAL(cursorPositionChanged(int,int)),SLOT(getHostInformation()));

}

HoInfo::~HoInfo()
{

}

void HoInfo::getHostInformation()
{
    QString localHostName = QHostInfo::localHostName();
    hostLE->setText(localHostName);

    QHostInfo hostInfo = QHostInfo::fromName(localHostName);
    QList<QHostAddress>listAddress = hostInfo.addresses();
    if(listAddress.isEmpty())
    {
        IpLE->setText(listAddress.first().toString());
    }

}

void HoInfo::slotDetail()
{
    QString detail = "";
    QList<QNetworkInterface>list = QNetworkInterface::allInterfaces();
    for(int i=0;i<list.count();i++)
    {
        QNetworkInterface interface = list.at(i);
        detail = detail+tr("Device:")+interface.name()+"\n";
        QList<QNetworkAddressEntry>entryList = interface.addressEntries();
        for(int j=0;j<entryList.count();j++)
        {
            QNetworkAddressEntry entry = entryList.at(j);
            detail = detail+"\t"+tr("IP Address:")+entry.ip().toString()+"\n";
            detail = detail+"\t"+tr("Netmask:")+entry.netmask().toString()+"\n";
            detail = detail+"\t"+tr("Broadcast:")+entry.broadcast().toString()+"\n";

        }
    }

    QMessageBox::information(this,tr("Detail"),detail);
}











posted on 2012-06-29 09:26 和总 阅读(237) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理


<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔档案

文章档案

相册

个人空间

搜索

  •  

最新评论