QT DBUS NetworkManager从IP4Config接口获取地址
一段时间以来,我一直在使用QTDBUS
和NetworkManager
,但遇到了问题。我需要访问Wi-Fi设备IP地址,获取它的方法是获取Wi-Fi设备对象,获取其ip4Config
属性(QDBUSOBJECTPATH对象),然后获取addressData
。问题是,一旦我尝试使用接口属性
方法获得地址数据,我的应用程序就会崩溃。
我相信我需要注册一种元类型(我已经为其他类型做了此类型),但似乎无法弄清楚如何解开aa {sv}
从属性返回的数据。使用QDBUSVIEWER
我可以看到IP地址的数据。
如何从该物业中获取此信息?
这是遇到问题的代码段。看来我可以获得Gateway
的QString属性,但是一旦我尝试获得addressData
我的进程崩溃(如下所示)。
cout << wifi_args.size() << endl;
cout << wifi_args[0].path().toStdString() << endl;
if (wifi_args.size() > 0) {
QDBusInterface wifi_device(NM_DBUS_SERVICE, wifi_args[0].path(),
NM_DBUS_INTERFACE_DEVICE,
QDBusConnection::systemBus());
const auto wifiInterface = wifi_device.property("Interface").toString();
// auto wifiIPv4 = wifi_device.property("Ip4Address").toInt();
auto ip4_config_path =
qdbus_cast<QDBusObjectPath>(wifi_device.property("Ip4Config"));
cout << "config path = " << ip4_config_path.path().toStdString()
<< endl;
QDBusInterface wifi_ip4_if(NM_DBUS_SERVICE, ip4_config_path.path(),
NM_DBUS_INTERFACE_IP4_CONFIG,
QDBusConnection::systemBus());
auto gateway = wifi_ip4_if.property("Gateway").toString();
auto ipv4 = wifi_ip4_if.property("AddressData");
// for (auto item : wifiIPv4) {
// cout << "Item";
// }
cout << "Interface = " << wifiInterface.toStdString() << endl;
cout << "Gateway = " << gateway.toStdString() << endl;
// cout << "Address = " << int_to_ipv4(wifiIPv4) << endl;
} else {
cerr << "ERROR: Multiple wifi devices found!" << endl;
}
这是运行中的输出:
michael_uman@ThinkPad-X1-Carbon-7th ~/gitroot/UXHUB-development (development)$ ./build-host/app/kylixctl/kylixctl list wifi
1
/org/freedesktop/NetworkManager/Devices/3
config path = /org/freedesktop/NetworkManager/IP4Config/8
Cannot construct placeholder type QDBusRawType
Aborted (core dumped)
I have been using QtDBus
and NetworkManager
for a while but have run into a problem. I need to access the Wi-Fi devices IP address and the way to obtain it is to get the Wi-Fi device object, get its IP4Config
property (a QDbusObjectPath object) and then to get the AddressData
. The problem is that as soon as I try to obtain the AddressData using the interfaces property
method, my application crashes.
I believe I need to register a meta type (I have done this for other types) but can't seem to figure out how to unpack the aa{sv}
data returned from the property. Using qdbusviewer
I can see the data for the IP address.
How can I get this information from the property?
Here is a snippet of the code which is encountering the problem. It seems I can get the QString property of Gateway
but as soon as I try to get the AddressData
my process crashes (as shown below).
cout << wifi_args.size() << endl;
cout << wifi_args[0].path().toStdString() << endl;
if (wifi_args.size() > 0) {
QDBusInterface wifi_device(NM_DBUS_SERVICE, wifi_args[0].path(),
NM_DBUS_INTERFACE_DEVICE,
QDBusConnection::systemBus());
const auto wifiInterface = wifi_device.property("Interface").toString();
// auto wifiIPv4 = wifi_device.property("Ip4Address").toInt();
auto ip4_config_path =
qdbus_cast<QDBusObjectPath>(wifi_device.property("Ip4Config"));
cout << "config path = " << ip4_config_path.path().toStdString()
<< endl;
QDBusInterface wifi_ip4_if(NM_DBUS_SERVICE, ip4_config_path.path(),
NM_DBUS_INTERFACE_IP4_CONFIG,
QDBusConnection::systemBus());
auto gateway = wifi_ip4_if.property("Gateway").toString();
auto ipv4 = wifi_ip4_if.property("AddressData");
// for (auto item : wifiIPv4) {
// cout << "Item";
// }
cout << "Interface = " << wifiInterface.toStdString() << endl;
cout << "Gateway = " << gateway.toStdString() << endl;
// cout << "Address = " << int_to_ipv4(wifiIPv4) << endl;
} else {
cerr << "ERROR: Multiple wifi devices found!" << endl;
}
Here is the output from the run:
michael_uman@ThinkPad-X1-Carbon-7th ~/gitroot/UXHUB-development (development)$ ./build-host/app/kylixctl/kylixctl list wifi
1
/org/freedesktop/NetworkManager/Devices/3
config path = /org/freedesktop/NetworkManager/IP4Config/8
Cannot construct placeholder type QDBusRawType
Aborted (core dumped)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过使用
org.freedesktop.dbus.properties.get
安全地获取有关属性,然后手动demarshall响应,类似于致命的错误,试图使用自定义类型获得DBU属性所需的数据而不会崩溃QT。
You can safely get the property in question by using
org.freedesktop.DBus.Properties.Get
and then manually demarshall the response, similar to Fatal error when trying to get a DBus property with custom typeThis should be sufficient to get the required data without crashing Qt.