[QT]QT教程之实例分析[八]获取IP (QHostInfo)时间:2021-04-19 05:30:35重点知识已近在代码里注释... 请仔细看代码 本文原创 转载请保留此链接 http://blog.csdn.net/siren0203 #include <QApplication> #include <QHostInfo> #include <QNetworkInterface> #include <QMessageBox> #include <QLineEdit> #include <QDialog> #include <QVBoxLayout> #include <QList> class NetWork :public QDialog{ public : NetWork(); }; NetWork::NetWork(){ QString info=QHostInfo::localHostName(); //获取主机名 QHostInfo hostinfo=QHostInfo::fromName(info); //通过主机名查找用户IP QList<QHostAddress> address=hostinfo.addresses(); //获取主机的IP地址 //实例文本编辑框 QLineEdit *lineEdit=new QLineEdit; QLineEdit *lineEdit2=new QLineEdit; //设置文本 lineEdit->setText(info); //转换成字符型 lineEdit2->setText(address.first().toString()); QVBoxLayout *layout=new QVBoxLayout; layout->addWidget(lineEdit); layout->addWidget(lineEdit2); setLayout(layout); } int main (int argc,char ** argv){ QApplication app(argc,argv); NetWork main; main.show(); return app.exec(); }