我使用以下代码获取Qt中的MAC ID.
main.cpp中
#include <QtCore/QCoreApplication>
#include "QtNetwork/QNetworkInterface"
#include "QString"
QString getMacAddress()
{
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
{
// Return only the first non-loopback MAC Address
if (!(interface.flags() & QNetworkInterface::IsLoopBack))
return interface.hardwareAddress();
QString text = interface.hardwareAddress();
qDebug() << text;
}
return QString();
}
int main(int argc, char *argv[])
{
getMacAddress();
QCoreApplication a(argc, argv);
return a.exec();
}
我在控制台中什么都没得到?引导我,谢谢……
最佳答案 尝试此代码,以显示每个接口的硬件地址:
QString getMacAddress()
{
QString text;
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
{
text += "Interface:"+interface.hardwareAddress()+"\n";
}
return text;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf( "%s\n", getMacAddress().toAscii().constData() );
exit(1);
return a.exec();
}