Qt 获取IP地址问题

marcomo1986 2011-06-08 02:45:50
我要获取主机的IP地址,我上网搜了搜写了下面几句话
#include <qhostinfo.h>
#include <qhostaddress.h>


QString str=QHostInfo::localHostName();
QHostInfo info=QHostInfo::fromName(str);
QHostAddress address=info.addresses().first();
str=address.toString();
结果出现了下面的错误:
Makefile:120: warning: overriding commands for target `moc_record.cpp'
Makefile:117: warning: ignoring old commands for target `moc_record.cpp'
g++ -c -pipe -Wall -W -O2 -DNO_DEBUG -I/arm/qt-2.3.x/qt-2.3.2/include -o main.o main.cpp
g++ -c -pipe -Wall -W -O2 -DNO_DEBUG -I/arm/qt-2.3.x/qt-2.3.2/include -o record.o record.cpp
record.cpp:19:23: qhostinfo.h: 没有那个文件或目录
record.cpp: In constructor `Form1::Form1(QWidget*, const char*, bool, unsigned
int)':
record.cpp:39: error: `QHostInfo' undeclared (first use this function)
record.cpp:39: error: (Each undeclared identifier is reported only once for
each function it appears in.)
record.cpp:39: error: syntax error before `::' token
record.cpp:42: error: `info' undeclared (first use this function)
make: *** [record.o] Error 1
我的头文件已经加了问什么还有这个错误啊?请高手指点下,分不多了,不好意思
...全文
538 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qudongtianxia 2011-06-08
  • 打赏
  • 举报
回复
同意楼上的观点,你很有可能没有添加network模块。另外还要在.pro文件中加上QT += network
_小青年儿_ 2011-06-08
  • 打赏
  • 举报
回复

99.#include <QNetworkInterface>
100.QString getIPAddress()
101.{
102.QString ipAddress;
103. QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
104. for (int i = 0; i < ipAddressesList.size(); ++i) {
105. if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
106. ipAddressesList.at(i).toIPv4Address()) {
107. ipAddress = ipAddressesList.at(i).toString();
108. break;
109. }
110. }
111. if (ipAddress.isEmpty())
112. ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
113. return ipAddress;
114.}

另外还要在.pro文件中加上QT += network
手机写程序 2011-06-08
  • 打赏
  • 举报
回复
搜下qhostinfo.h,有没有。
haixuelang 2011-06-08
  • 打赏
  • 举报
回复
可能是要在.pro文件中添加一些东西,比如qt要使用sqlite数据库就必须在.pro文件加上 QT +=sql

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧