-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiphandler.cpp
More file actions
46 lines (40 loc) · 1.93 KB
/
Copy pathiphandler.cpp
File metadata and controls
46 lines (40 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "iphandler.h"
#include <QDebug>
IpHandler::IpHandler(QObject *parent) : QObject(parent)
{
}
QString IpHandler::getWifiIPAddress()
{
QStringList wifiInterfaceNames = { "wlan0", "wlp", "wl", "en0", "WiFi" }; // WiFi names to check
// qDebug() << "Listing all available network interfaces:";
// foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
// qDebug() << "Interface Name:" << interface.humanReadableName();
// foreach (const QNetworkAddressEntry &entry, interface.addressEntries()) {
// if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
// qDebug() << " IPv4 Address:" << entry.ip().toString();
// }
// }
// }
// Now try to match the WiFi interface
foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
// Check if this interface is up and running, and not a loopback interface
if (interface.flags().testFlag(QNetworkInterface::IsUp) &&
interface.flags().testFlag(QNetworkInterface::IsRunning) &&
!interface.flags().testFlag(QNetworkInterface::IsLoopBack)) {
// Match the WiFi interface name from the known WiFi interface names
for (const QString &wifiName : wifiInterfaceNames) {
if (interface.humanReadableName().startsWith(wifiName)) {
foreach (const QNetworkAddressEntry &entry, interface.addressEntries()) {
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
QString ip = entry.ip().toString();
qDebug() << "WiFi Interface:" << interface.humanReadableName() << "IP Address:" << ip;
return ip;
}
}
}
}
}
}
qDebug() << "No WiFi IP found";
return QString("No WiFi IP found");
}