我正在查询NetworkManager的org.freedesktop.NetworkManager.Settings.Connection
界面,调用" GetSettings"在上面。它以Dbus类型术语返回Dict of {String, Dict of {String, Variant}}
或a{sa{sv}}
。我正在使用QtCreator和Qt4来构建我的应用程序。
我似乎无法从这本词典中获得一些可用的信息。 如果NetworkManager和DBus和Qt4安装在某人的系统上,我无法提供MVE,因为它非常依赖。
这是我开发的方法,用于从字典和字典和变体字典中获取信息。在将它汇总到qDebug()时,我可以看到我想要的所有好数据:qDebug()<<reply
。
void GetInfo()
{
//sysbus just means the system DBus.
QDBusInterface connSettings("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings/1", "org.freedesktop.NetworkManager.Settings.Connection", sysbus);
QDBusMessage reply = connections.call("GetSettings");
if(reply.type() == QDBusMessage::ReplyMessage)
{
//I have tried everything I can imagine here,
//QVariant g = reply.arguments().at(0).value<QVariant>(); did not work
//g.canConvert<QMap>(); returns false, in contrast to what KDE says.
//QDbusArgument g = query.arguments().at(0).value<QDBusArgument>();
//g.beginMap(); and such don't work
}
}
很难找到解析Dict类型的信息。我发现提供一些信息的唯一来源是KDE。它说&#34; DBus Dict类型应该映射到QMap,示例如下:&#34;并且没有其他点击谷歌或示例存在。也许我错过了一些有趣的数据库知识,但我很难过。
我也检查了这个优秀的答案:How do I extract the returned data from QDBusMessage in a Qt DBus call?但是我无法调整它来解析字典。
有人知道如何到达最后一个嵌套的QVariant吗?
答案 0 :(得分:5)
不幸的是,Qts DBUS API并不总是最容易理解的,所以这里有一些提示。基本上我发现对我有用的是你必须从回复中获得operator<<
,这才是获取实际数据的实际用途。根据您提取的内容,您可以覆盖.cpp文件中的operator>>
和QDBusReply::arguments()
(the documentation for QDBusArgument says how to do this),也可以定义要提取的类型。另一个需要注意的重要事项是QDBusInterface connSettings("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager/Settings/1",
"org.freedesktop.NetworkManager.Settings.Connection",
QDBusConnection::systemBus() );
QDBusMessage reply = connSettings.call("GetSettings");
qDebug() << "Reply below:";
qDebug() << reply;
qDebug() << "Extracted: ";
// Extract the argument from the reply
// In this case, we know that we get data back from the method call,
// but a range check is good here as well(also to ensure that the
// reply is a method reply, not an error)
const QDBusArgument &dbusArg = reply.arguments().at( 0 ).value<QDBusArgument>();
// The important part here: Define the structure type that we want to
// extract. Since the DBus type is a{sa{sv}}, that corresponds to a
// Map with a key of QString, which maps to another map of
// QString,QVariant
QMap<QString,QMap<QString,QVariant> > map;
dbusArg >> map;
qDebug() << "Map is: " << map;
// We're just printing out the data in a more user-friendly way here
for( QString outer_key : map.keys() ){
QMap<QString,QVariant> innerMap = map.value( outer_key );
qDebug() << "Key: " << outer_key;
for( QString inner_key : innerMap.keys() ){
qDebug() << " " << inner_key << ":" << innerMap.value( inner_key );
}
}
contains either 发送或接收参数,具体取决于是回复还是通话。
无论如何,以下内容适用于我(虽然在Qt5中,但我希望它也适用于Qt4)
log:connect() index.js:32
log:connect() bypassed channel-test. index.js:32
log:connectTest_() index.js:32
log:GetForwardChannelUri: https://firestore.googleapis.com/google.firestore.v1beta1.Firestore/Listen/channel/test?VER=8 index.js:32
log:TestConnection: starting stage 2 index.js:32
log:Buffered index.js:32
log:Test Connection Finished index.js:32
log:connectChannel_() index.js:32
log:GetForwardChannelUri: https://firestore.googleapis.com/google.firestore.v1beta1.Firestore/Listen/channel?VER=8