Add function to get mac address

This commit is contained in:
Jonas Kvinge 2019-09-22 17:04:57 +02:00
parent a1745289be
commit c146290e07
1 changed files with 23 additions and 0 deletions

View File

@ -59,6 +59,7 @@
#include <QSettings> #include <QSettings>
#include <QtEvents> #include <QtEvents>
#include <QMessageBox> #include <QMessageBox>
#include <QNetworkInterface>
#include <QtDebug> #include <QtDebug>
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
@ -815,6 +816,28 @@ QString UnicodeToAscii(const QString &unicode) {
} }
QString MacAddress() {
QString ret;
for (QNetworkInterface &interface : QNetworkInterface::allInterfaces()) {
if (
(interface.hardwareAddress() == "00:00:00:00:00:00") ||
(interface.flags() & QNetworkInterface::IsLoopBack) ||
!(interface.flags() & QNetworkInterface::IsUp) ||
!(interface.flags() & QNetworkInterface::IsRunning)
) { continue; }
if (ret.isEmpty() || interface.type() == QNetworkInterface::Ethernet || interface.type() == QNetworkInterface::Wifi) {
ret = interface.hardwareAddress();
}
}
if (ret.isEmpty()) ret = "00:00:00:00:00:00";
return ret;
}
} // namespace Utilities } // namespace Utilities
ScopedWCharArray::ScopedWCharArray(const QString &str) ScopedWCharArray::ScopedWCharArray(const QString &str)