【发布时间】:2013-12-01 07:41:22
【问题描述】:
我正在尝试使用 NetworkManager DBus 接口为我在 Qt 中的所有接口获取网络信息(IP 地址、网络掩码、路由等)。问题是当我尝试访问 org.freedesktop.NetworkManager.IP4Config 的属性“地址”时,出现以下错误
QDBusAbstractInterface: type QDBusRawType<0x616175>* must be registered with QtDBus before it can be used to read property org.freedesktop.NetworkManager.IP4Config.Addresses
Addresses are invalid
Error 2 = "Unregistered type QDBusRawType<0x616175>* cannot be handled"
但是我可以使用 dbus-send 和以下命令获取此属性的值。
dbus-send --system --print-reply --dest=org.freedesktop.NetworkManager \
/org/freedesktop/NetworkManager/IP4Config/0 \
org.freedesktop.DBus.Properties.Get \
string:"org.freedesktop.NetworkManager.IP4Config" \
string:"Addresses"
我还可以通过 qtdbusviewer 获得上述接口提到的属性的良好值。以下是我的代码 sn-p。
QDBusInterface interface(NM_DBUS_SERVICE, NM_DBUS_PATH, NM_DBUS_IFACE, QDBusConnection::systemBus());
// Get a list of all devices
QDBusReply<QList<QDBusObjectPath> > result = interface.call("GetDevices");
foreach (const QDBusObjectPath& connection, result.value()) {
QDBusInterface device(NM_DBUS_SERVICE, connection.path(), "org.freedesktop.NetworkManager.Device", QDBusConnection::systemBus());
if ( device.property("DeviceType").toInt() == NM_DEVICE_TYPE_ETHERNET ) {
// Get the IPv4 information if the device is active
if ( device.property("State").toInt() == NETWORK_DEVICE_CONNECTED ) {
QVariant ipv4config = device.property("Ip4Config");
if ( ipv4config.isValid() ) {
QDBusObjectPath path = qvariant_cast<QDBusObjectPath>(ipv4config);
QDBusInterface ifc(NM_DBUS_SERVICE, path.path(), "org.freedesktop.NetworkManager.IP4Config", QDBusConnection::systemBus());
if ( ifc.isValid() ) {
qDebug() << "Error 1 = " << ifc.lastError().message(); // No error. Everything is OK.
QVariant addresses = ifc.property("Addresses"); // Throwing the QDBusAbstractInterface Error where the property is good and does exist.
if ( addresses.isValid() ) {
qDebug () << "Addresses are valid";
} else {
qDebug () << "Addresses are invalid";
}
qDebug() << "Error 2 = " << ifc.lastError().message();
}
}
}
}
}
更新 #1
我认为这似乎是类型的问题。 Qt-Dbus 类型系统不理解“地址”属性的类型,因此无法从中创建 QVariant。所以我在阅读属性“地址”之前添加了以下几行。 NetworkManager 将属性 Addresses 定义为以下类型,所以我想我的 typedef 很好。
aau - “IPv4 地址/前缀/网关的元组数组。每个元组的所有 3 个元素都按网络字节顺序排列。本质上:[(addr, prefix, gateway), (addr, prefix, gateway), .. .]"
typedef QList<QList<uint> > Addresses;
Q_DECLARE_METATYPE(Addresses)
qDBusRegisterMetaType<Addresses>()
QVariant addresses = ifc.property("Addresses");
我也切换到 Qt 5.1(之前我使用的是 4.8),并且我在以下表单中遇到了同样的错误。
Cannot construct placeholder type QDBusRawType
想法/建议
问候, 法鲁克·阿尔沙德。
【问题讨论】:
标签: qt properties dbus