我正在查询NetworkManager的org.freedesktop.NetworkManager.Settings.Connection接口(interface),在其上调用“GetSettings”。它以Dbus类型术语返回Dict of {String, Dict of {String, Variant}}a{sa{sv}}。我正在将QtCreator与Qt4一起使用来构建我的应用程序。

我似乎无法从这本词典中得到一些有用的信息。
我无法提供MVE,因为如果在某人的系统上安装了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。它说:“DBus Dict类型应该映射到QMap,后面要跟随示例。”并且在Google上没有其他匹配项或示例。也许我缺少一些基本的DBus知识,但是我很困惑。

我也 checkout 了这个出色的答案:How do I extract the returned data from QDBusMessage in a Qt DBus call?,但是我无法适应它来解析字典。

有谁知道如何到达最后一个嵌套的QVariant?

最佳答案

不幸的是,Qts DBUS API并非总是最容易理解的,因此这里有一些技巧。基本上,我发现对我有用的是,您将必须从回复中获取DBusArgument,这就是为了获取实际数据而实际使用的内容。根据要提取的内容,您可以覆盖.cpp文件中的operator<<operator>>(the documentation for QDBusArgument says how to do this),也可以定义要提取为的类型。另一个需要注意的重要事项是QDBusReply::arguments() contains either发送或接收参数,具体取决于它是答复还是调用。

无论如何,以下内容对我有用(尽管在Qt5中,但我希望它也可以在Qt4中使用)

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 );
    }
}

10-01 08:36