• qt获取本机网络信息


    networkinformation.h

    #include<QtGui/QWidget>
    #include<QLabel>
    #include<QPushButton>
    #include<QLineEdit>
    #include<QGridLayout>
    #include<QHostInfo>
    
    
    class NetworkInformation:public QWidget
    {
    	Q_OBJECT
    	
    	public:
    		NetworkInformation(QWidget *parent=0);
    		void getHostInformation();
    		//~NetworkInformation();
    	
    	public slots:
    		void slotDetail();
    
    	private:
    		QLabel *hostLabel;
    		QLineEdit *LineEditLocalHostName;
    		QLabel *ipLabel;
    		QLineEdit *LineEditAddress;
    		QPushButton *detailBtn;
    		QGridLayout *mainLayout;
    };

    networkinformation.cpp:

    #include<QNetworkInterface>
    #include<QMessageBox>
    #include"networkinformation.h"
    
    
    NetworkInformation::NetworkInformation(QWidget *parent)
    	:QWidget(parent)
    {
    	hostLabel=new QLabel(tr("host:"));
    	LineEditLocalHostName=new QLineEdit;
    	ipLabel=new QLabel(tr("IP:"));
    	LineEditAddress=new QLineEdit;
    
    	detailBtn=new QPushButton(tr("details"));
    
    	mainLayout=new QGridLayout(this);
    	mainLayout->addWidget(hostLabel,0,0);
    	mainLayout->addWidget(LineEditLocalHostName,0,1);
    	mainLayout->addWidget(ipLabel,1,0);
    	mainLayout->addWidget(LineEditAddress,1,1);
    	mainLayout->addWidget(detailBtn,2,0,1,2);
    
    	getHostInformation();
    
    	connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));
    }
    
    
    void NetworkInformation::getHostInformation()
    {
    	QString localHostName=QHostInfo::localHostName();
    	LineEditLocalHostName->setText(localHostName);
    
    	QHostInfo hostInfo=QHostInfo::fromName(localHostName);
    	QList<QHostAddress> listAddress=hostInfo.addresses();
    
    	if(!listAddress.isEmpty())
    	{
    		LineEditAddress->setText(listAddress.first().toString());
    	}
    }
    
    
    void NetworkInformation::slotDetail()
    {
    	QString detail="";
    	QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
    
    	for(int i=0;i<list.count();i++)
    	{
    		QNetworkInterface interface=list.at(i);
    		detail=detail+tr("shebei:")+interface.name()+"
    ";
    		QString hardwareAddress=interface.hardwareAddress();
    		detail=detail+tr("hardware address:")+interface.hardwareAddress()+"
    ";
    		QList<QNetworkAddressEntry> entryList=interface.addressEntries();
    
    		for(int j=0;j<entryList.count();j++)
    		{
    			QNetworkAddressEntry entry=entryList.at(j);
    			detail=detail+"	"+tr("ip address:")+entry.ip().toString()+"
    ";
    			detail=detail+"	"+tr("netmask:")+entry.netmask().toString()+"
    ";
    			detail=detail+"	"+tr("broadcast:")+entry.broadcast().toString()+"
    ";
    		}
    	}
    	QMessageBox::information(this,tr("Detail"),detail);
    }

    main.cpp:

    #include<QtGui/QApplication>
    #include"networkinformation.h"
    #include<QTextCodec>
    
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc,argv);
    	QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    	NetworkInformation w;
    	w.show();
    	return a.exec();
    }


  • 相关阅读:
    文本PDG文件名构成
    关于文本PDG的字体
    Oracle 11G R2 在windows server 2008 64位安装时提示:无法在windows "开始"菜单或桌面上创建项
    GTONE上安装插件无法显示SecurityPrism菜单
    Centos系统下Lamp环境的快速搭建(超详细)
    Windows 10激活
    word如何让单页变横向
    redhat 6.x 上创建用户
    redhat下网络的配置
    Windows 10、Windows 2012 安装 Oracle 11g 报错:[INS-13001]环境不满足最低要求。
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5399626.html
Copyright © 2020-2023  润新知